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

        /// <summary>
        /// 退出应用的的时候执行
        /// 目的是做推出应用保存数据用的
        /// </summary>
        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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值