Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
/ unity-mvc Public archive

Implementation of MVC (Model-View-Controller) architectural pattern via Unity engine

License

Notifications You must be signed in to change notification settings

dancher743/unity-mvc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Important

This project is no longer supported. Try MVP instead of MVC.

unity-mvc

Implementation of MVC (Model-View-Controller) architectural pattern via Unity engine.

Getting Started

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!

Create a Model

Implement IModel interface to create a Model -

public class CubeModel : IModel
{
	  ...
}

Create a View

Implement IView interface to create a View -

public class CubeView : MonoBehaviour, IView
{
	  ...
}

Create a Controller

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)
  	{
      		...
  	}
}

Create a Controller instance

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())

Remove a Controller instance

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!

Messaging

Receive a Message

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.

Dispatch a 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

Example project with the latest version of the package is available here.