Unity-MVP

using UnityEngine;

namespace MVP
{
    public class AllBase : MonoBehaviour
    {
        private void Awake() {
            OnAwake();
        }
        protected virtual void OnAwake() { }

        private void Start() {
            OnStart();
        }
        protected virtual void OnStart() {

        }

        protected virtual void OnEnable() { }

        private void Update() {
            OnUpdate();
        }

        protected virtual void OnUpdate() { }

        protected virtual void OnDisable() { }

        protected virtual void OnDestroy() { }

        private void OnApplicationQuit() {
            OnQuit();
        }

        /// 
        /// 退出应用的的时候执行
        /// 目的是做推出应用保存数据用的
        /// 
        protected virtual void OnQuit() { }
    }
}
namespace MVP
{
    public interface IModelBase
    {

    }
}

namespace MVP
{
    public interface IViewBase
    {

    }
}

namespace MVP
{
    public abstract class ViewPresenterBase<V, M> : AllBase
    where V : IViewBase
    where M : IModelBase
    {
        internal M model;
        internal V view;

        protected override void OnAwake() {
            base.OnAwake();
            Init();
        }

        private void Init() {
            model = (M)GetComponent<IModelBase>();
            view = (V)GetComponent<IViewBase>();
        }
    }
}

使用

using System;
using UnityEngine;
namespace MVP
{
    public class SetupData : AllBase, IModelBase
    {
        public Action<string> OnNameChanged;
        private string m_name;
        public string Name {
            get
            {
                return m_name;
            }
            set
            {
                if (value != m_name) {
                    m_name = value;
                    OnNameChanged?.Invoke(value);
                }
            }
        }
        public int Age { get; set; }
        public State State { get; set; }

        public void DebuName() {
            Debug.Log("Name:" + Name);
        }
    }
    public enum State
    {
        JoinIn, Wait
    }
}

namespace MVP
{
    public class SetupPresenter:ViewPresenterBase<SetupPanel,SetupData>
    {
        protected override void OnAwake() {
            base.OnAwake();
            view.OnValueChanged += SetName;
            model.OnNameChanged += OnNameChanged;
        }
        public void SetName(string name) {
            model.Name = name;
            model.DebuName();
        }
        public void OnNameChanged(string name) {
            view.SetTextValue(name);
        }
        public void SetAge(int age) {
            model.Age = age;
        }
    }
}

using System;
using UnityEngine;
using UnityEngine.UI;
namespace MVP
{
    public class SetupPanel :MonoBehaviour, IViewBase
    {
        public Action<string> OnValueChanged;
        public InputField input;
        public Text text;
        void Awake() {
            this.gameObject.AddComponent<SetupData>();
            this.gameObject.AddComponent<SetupPresenter>();
            RegistEvent();
        }

        void RegistEvent() {
            input.onValueChanged.AddListener(val => {
                OnValueChanged?.Invoke(val);
            });
        }
        public void SetTextValue(string value) {
            text.text = value;
        }
    }
}

你可能感兴趣的:(unity,游戏引擎)