Important
This project is no longer supported. Try MVP instead of MVC.
Implementation of MVC (Model-View-Controller) architectural pattern via Unity engine.
To use MVC we should create Model, View and Controller, setup relations between these three and setup messages between Controllers.
We recommend to get example project before we started.
Let's get started!
Implement IModel
interface to create a Model -
public class CubeModel : IModel
{
...
}
Implement IView
interface to create a View -
public class CubeView : MonoBehaviour, IView
{
...
}
Derived from Controller<TView, TModel>
class and specifies types: TView
and TModel
to create a Controller.
In our case TModel
is CubeModel
and TView
is CubeView
-
CubeController : Controller<CubeView, CubeModel>
public class CubeController : Controller<CubeView, CubeModel>
{
public CubeController(CubeView cubeView, CubeModel cubeModel) : base(cubeView, cubeModel)
{
...
}
}
When we’re done with Model, View and Controller next step is going to be a creation of Controller instance.
To achieve this, use CreateController<TController>(IView view, IModel model)
method of ControllerManager
static class -
ControllerManager.CreateController<CubeController>(cubeView, new CubeModel())
[SerializeField]
private CubeView cubeView;
...
void Start()
{
cubeController = ControllerManager.CreateController<CubeController>(cubeView, new CubeModel());
}
You also able to create Controller instance using classic approuch without ControllerManager
-
var cubeController = new CubeController(cubeView, new CubeModel())
To remove some Controller from ControllerManager
use RemoveController<TController>(TController controller)
method -
private void OnDestroy()
{
ControllerManager.RemoveController(cubeController);
ControllerManager.RemoveController(uiController);
}
NOTE: You should pass Controller instance (not generic type) to RemoveController
method to remove it from ControllerManager
.
Also you can implement an ICleareable
interface to make additional clean before Controller will be removed. Clear()
method will be called by ControllerManager
automatically.
public interface ICleareable
{
void Clear();
}
public class CubeController : Controller<CubeView, CubeModel>, ICleareable
{
public CubeController(CubeView cubeView, CubeModel cubeModel) : base(cubeView, cubeModel)
{
view.Clicked += OnViewClicked;
model.ColorChanged += OnModelColorChanged;
}
public void Clear()
{
view.Clicked -= OnViewClicked;
model.ColorChanged -= OnModelColorChanged;
}
...
}
We're done!
Every Controller
can message with other controllers.
Implement IMessageReceivable
interface to make class available for message receiving -
public interface IMessageReceivable
{
void ReceiveMessage<TMessageData>(TMessageData data) where TMessageData : struct;
}
public class UIController : Controller<UIView, UIModel>, IMessageReceivable
{
...
public void ReceiveMessage<TMessageData>(TMessageData data)
{
switch (data)
{
case CubeColorData cubeColorData:
model.Color = cubeColorData.Color;
break;
}
}
}
Block case CubeColorData cubeColorData
used here as a way to handle some message.
To dispatch a Message to some controller use DispatchMessageTo<TController, TMessageData>(TMessageData data)
method of ControllerManager
-
ControllerManager.DispatchMessageTo<UIController, CubeColorData>(new CubeColorData(color))
public class CubeController : Controller<CubeView, CubeModel>, ICleareable
{
...
private void OnModelColorChanged(Color color)
{
view.Color = color;
ControllerManager.DispatchMessageTo<UIController, CubeColorData>(new CubeColorData(color));
}
}
Example project with the latest version of the package is available here.