Search Results for

    Omiya Games - MVC

    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

    Through Unity Package Manager

    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:

    • Model
    • View
    • Controllers

    Sample Code

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

    Model

    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);
        }
    }
    

    View

    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);
        }
    }
    

    Controller

    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

    • Documentation
    • Change Log

    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

    • Improve this Doc
    In This Article
    Back to top Copyright © 2021-2022 Omiya Games