- Home /
Unity MVC get View atached to the UI label
A simple task. I have a ScoreController which will set the score to UI. I have a UI label that has ScoreView in it
The question is how do I get this ScoreView in my controller
I have found out the way to get View from prefab that's being instantiated:
public class CellViewFactory : ICellViewFactory
{
public ICellView GetView(Vector3 position)
{
var prefab = Resources.Load<GameObject>("Cell");
var instance = UnityEngine.Object.Instantiate(prefab);
var cellView = instance.GetComponent<ICellView>();
cellView.SetPosition(position);
return cellView;
}
}
But since UI isn't instantiated by the code, what's the way to get the ScoreView from it?
Now, I have found an option to call a static method of a Factory and pass self to the method. Smth like this:
public class ScoreView : MonoBehaviour
{
void Start()
{
ScoreViewFactory.NotifyScoreViewCreated(this);
}
}
But it doesn't seem to be flexible using static methods in decoupled code with Factories and interfaces. Hope there's another way
I would also be glad if you post some complete tutorioal on MVC in Unity. Seems that there's no complete working tutorials.
It would also be great if you would share an example of MVC pattern in Unity using DependencyInjection.
Your answer
Follow this Question
Related Questions
How can I know if a gameObject is 'seen' by a particular camera? 11 Answers
Object material showing in scene but not game view 2 Answers
Changing the appearance of particles from another player's view 4 Answers
Can't See Anything 1 Answer
View through objects 2 Answers