Skip to content

OmiyaGames/omiya-games-mvc

Repository files navigation

openupm MVC Package documentation Ko-fi Badge License Badge

The Model-View-Controller (MVC) framework is a common way of organizing code for GUI applications. This package implements a number of helper scripts to help enforce this framework for a Unity project. Currently, this package is in development stages, and may change over time.

This MVC implementation runs with the philosophy that Models contains data, delegates, and [ContextMenu] methods for implementing quick cheats. Controllers, meanwhile, creates and sets up models with initial data, and assigning functions to delegates that manipulates the model's data. Finally, Views grabs instances of models to update visuals (e.g. UI) in-game based off of model's data, call the model's delegate, and listen to them like events.

With this organization, it's becomes possible to display in-game data in realtime through the use of the Model Inspector:

Model Inspector Preview

Install

Unity's own Package Manager supports importing packages through a URL to a Git repo:

  1. First, on this repository page, click the "Clone or download" button, and copy over this repository's HTTPS URL.
  2. Then click on the + button on the upper-left-hand corner of the Package Manager, select "Add package from git URL..." on the context menu, then paste this repo's URL!

While easy and straightforward, this method has a few major downside: it does not support dependency resolution and package upgrading when a new version is released. To add support for that, the following method is recommended:

About the Manual

Each part of the MVC framework are described in more thorough details in the links below:

Sample Code

Here's an example of reading a text input entry from a UI:

using OmiyaGames.MVC;
using UnityEngine;

public class CustomModel : Model
{
	// Serialized member variable
	public string text = "Testing!";

	// Delegate for the controller to define
	public Controller.EventBase<string> ChangeText;

	// Context Menu method, usually for implementing cheats
	[ContextMenu("Log Text")]
	public void LogText()
	{
		Debug.Log(text);
	}
}
using OmiyaGames.MVC;
using UnityEngine;
using UnityEngine.UI;

public class CustomView : MonoBehaviour
{
	CustomModel model;

	[SerializeField]
	TextInput input;

	void Start()
	{
		// Retrieve the CustomModel
		// Note: if ModelFactory.Create<CustomModel>() hasn't been called yet,
		// this line *will* throw an exception!
		model = ModelFactory.Get<CustomModel>();

		// Update text input value
		input.text = model.text;
	}

	// Called by the submit button
	public void OnSubmitClicked()
	{
		// Call ChangeText if it's defined
		model.ChangeText?.Invoke(this, input.text);
	}
}
using OmiyaGames.MVC;
using UnityEngine;

public class CustomController : MonoBehaviour
{
	CustomModel model;

	[SerializeField]
	string firstText = "First!";

	// Using Awake() so model is created before Start()
	void Awake()
	{
		// Create the CustomModel
		model = ModelFactory.Create<CustomModel>();

		// Setup initial data of the model
		model.text = firstText;
		model.ChangeText = (source, newText) => model.text = newText;
	}

	void OnDestroy()
	{
		// (Optional) Destroy the CustomModel
		ModelFactory.Release<CustomModel>();
		model = null;
	}
}

Resources

LICENSE

Overall package is licensed under MIT, unless otherwise noted in the 3rd party licenses file and/or source code.

Copyright (c) 2021-2022 Omiya Games

About

An attempt to implement model-view-controller in Unity.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages