Unity one-click generation of MVC framework

Too lazy to create a GameObject, to create a Tag, to set a Tag, to add a script, to get the quoted gospel of lazy cancer patients every time

Old rules, don’t want to watch the blog’s direct downloads, portal

Introduction to MVC : MVC

It is not recommended to use frameworks for small projects to avoid over-design.

Above code:
Model block: (data, logic layer)

using UnityEngine;
public class Model : MonoBehaviour
{
    
    
}

View block: (view layer)

using UnityEngine;
public class View : MonoBehaviour
{
    
    
    void Awake()
    {
    
    
    }
    void Start()
    {
    
    
    }
}

Control block: (control transfer layer)

using UnityEngine;
public class Control : MonoBehaviour
{
    
    
    private Model _model;
    internal Model Model
    {
    
    
        get
        {
    
    
            if (_model != null)
            {
    
    
                return _model;
            }
            else
            {
    
    
                _model = GameObject.FindWithTag("Model").GetComponent<Model>();
                return _model;
            }
        }
        set
        {
    
    
            if (_model != null)
            {
    
    
                _model = value;
            }
            else
            {
    
    
                _model = GameObject.FindWithTag("Model").GetComponent<Model>();
                _model = value;
            }
        }
    }
    private View _view;
    internal View View
    {
    
    
        get
        {
    
    
            if (_view != null)
            {
    
    
                return _view;
            }
            else
            {
    
    
                _view = GameObject.FindWithTag("View").GetComponent<View>();
                return _view;
            }
        }
        set
        {
    
    
            if (_view != null)
            {
    
    
                _view = value;
            }
            else
            {
    
    
                _view = GameObject.FindWithTag("View").GetComponent<View>();
                _view = value;
            }
        }
    }
}

BaseControl: (parent class, saves the trouble of getting the transfer control layer Control every time the controller needs to be inherited)

using UnityEngine;
public class BaseControl : MonoBehaviour
{
    
    
    private Control _ctrl;
    internal Control Ctrl
    {
    
    
        get
        {
    
    
            if (_ctrl != null)
            {
    
    
                return _ctrl;
            }
            else
            {
    
    
                _ctrl = GameObject.FindWithTag("Control").GetComponent<Control>();
                return _ctrl;
            }
        }
        set
        {
    
    
            if (_ctrl != null)
            {
    
    
                _ctrl = value;
            }
            else
            {
    
    
                _ctrl = GameObject.FindWithTag("Control").GetComponent<Control>();
                _ctrl = value;
            }
        }
    }
}

GameManager layer: (game control layer)

#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
public class GameManager : BaseControl
{
    
    
    void Awake()
    {
    
    
    }
    void Update()
    {
    
    
    }
    #region 动态创建tag、物体、挂载脚本
    [ContextMenu("添加设置标签")]
    void Add()
    {
    
    
#if UNITY_EDITOR
        AddTag<Model>("Model");
        AddTag<View>("View");
        AddTag<Control>("Control");
#endif
    }
    void AddTag<T>(string tag) where T : Component
    {
    
    
        if (!isHasTag(tag))
        {
    
    
            GameObject obj = new GameObject(tag);
            obj.AddComponent<T>();
            SerializedObject tagManager = new SerializedObject(obj);//序列化物体
            SerializedProperty it = tagManager.GetIterator();//序列化属性
            while (it.NextVisible(true))//下一属性的可见性
            {
    
    
                if (it.name == "m_TagString")
                {
    
    
                    //Debug.Log(it.stringValue);
                    it.stringValue = tag;
                    tagManager.ApplyModifiedProperties();
                }
            }
        }
    }
    bool isHasTag(string tag)
    {
    
    
        foreach (var t in UnityEditorInternal.InternalEditorUtility.tags)
        {
    
    
            if (t.Equals(tag))
                return true;
        }
        return false;
    }
    #endregion
}

What's the main thing:
Import the GameManager prefab, one-click to generate, mount, and set the object.
Look at the picture
One-click generation
Insert picture description here
, please accept lazy cancer patients

When it was created, it only made the judgment of no tag and then created it. No judgment is made for the existing tags. If you want to improve it by yourself, send me a copy after finishing it, thank you

Guess you like

Origin blog.csdn.net/gheartsea/article/details/108636920