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

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…