- Home /
How should I integrate the MVC pattern?
Hi,
First of all, I'm sorry for my bad english. I'm wondering which is the best way to implement the MVC architecture pattern in Unity. I'm newbie on using this software but I've some experience in the programming world.
At first, I suppose I need to create an empty GameObject with the main controller script in an empty scene . This controller, should request for each view / scene / level in the game with Application.loadLevel. With this, the controller is gonna be destroyed, so I need to tell in Awake() to DontDestroyOnLoad(this).
But, how can the controller handle the inputs received in the view? The view, needs to send a request to controller, telling him: "the user has clicked this button of mine". In this case, the controller is defined in, for example, Scene0, and the view in Scene01. If I try to GetComponet() from the View, Unity says me he can't get the controller instance.
Can you give me a little example of this architecture pattern working?
Thanks a lot
Answer by Immanuel-Scholz · Nov 20, 2014 at 08:31 PM
I am not sure you should use an Unity "scenes" as a view. To me, it makes more sense to group controller, view and models for an specific level into one scene.
There may be some model - gameObjects present over scenes (using dontdestroyonload). Just to pass on data between scenes.
Or you just could use plain C# classes not related to MonoBehaviour as data classes (although I recommend you use MonoBehaviours, just to save you some time writing editor code to debug the stuff).
Be aware that not much people use strict MVC coding patterns in Unity. Most people just go with the flow and put the stuff together somehow..
Your answer
Follow this Question
Related Questions
How do I hook custom inspectors up to models? 2 Answers
How to avoid one engine class controlling the game flow getting too large 3 Answers
Decoupling state & graphics, while sequentially executing graphic updates 1 Answer
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers