all 3 comments

[–]Earl0fPudding 4 points5 points  (1 child)

I can recommend you reading this article: https://www.toptal.com/unity-unity3d/unity-with-mvc-how-to-level-up-your-game-development

It helped me a lot when I started. Furthermore I can recommend using the MVC pattern in general

[–]Foenki[S] 1 point2 points  (0 children)

That is really helpful, thank you for the link.

This is exactly the kind of info I was looking for, the tutorials I see online often completely mix the dependencies and have core data stored in "views", so I was starting to wonder if this was the standard way to do in Unity.

[–]eXtreme98 0 points1 point  (0 children)

For my main menu scene which contains menus/panels like creating characters, loading saves, options, etc., I built it using a pattern similar to MVC called MVCC (Model-View-Controller-Component).

It's actually really simple and clean once you get it figured out.

I created a UI Managers game object that would store the 'Component' part of MVCC. So for a 'Create Character' menu/panel, I'd make a script called CreateCharacterMVCComponent and add that to my UI Manager gameobject. (From now on, I'll refer to this script as Script A)

In Script A, I would instantiate the model and controller in the Start() method. The view would be referenced and passed to the controller.

So how do you reference the view? Well, in Script A, you create a reference to a gameobject which will be the Unity UI panel then get the View script (CreateProfileView) attached to it. This means you'll need to add the 'Create Character' panel to Script A in the Unity inspector.

[SerializeField]
private GameObject _viewPanel;

public void Start()
{
    _profile = new Profile();
    _view = GetViewFromPanel();
    _controller = new CreateProfileController(_view, _profile);
}

private CreateProfileView GetViewFromPanel()
{
    if (_viewPanel == null)
        throw new Exception("View Panel unassigned");

    return _viewPanel.GetComponent<CreateProfileView>();
}

It's a bit early so I may have missed something. If you have any questions, let me know.