EeveeUI框架7——MVC中的V

V层主要是视图层,视图层主要解决了与UGUI的按钮绑定关系、还有刷新页面回调。V和C的交互则主要通过事件系统。

这里我们看一下V的代码,其实也很简单,分成了两块,一个是输入,一个是输出:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UI界面,及UI逻辑
/// 即为MVC的V层
/// </summary>
public class MainPanel_UIControl : UIControl
{
    protected new void Awake()
    {
        base.Awake();
        Init();
    }

    void Start()
    {

    }

    #region 界面内容及事件绑定
    private Text m_BloodVolumeText;
    private Button m_SubtractBloodButton;
    private Button m_AddBloodButton;
    /// <summary>
    /// 初始化获取界面内容、绑定按钮事件
    /// </summary>
    void Init()
    {
        m_BloodVolumeText = m_Children["MainPanel/BloodVolumeText"].GetComponent<Text>();
        m_SubtractBloodButton = m_Children["MainPanel/SubtractBloodButton"].GetComponent<Button>();
        m_AddBloodButton = m_Children["MainPanel/AddBloodButton"].GetComponent<Button>();

        //设置按钮点击发送事件
        m_SubtractBloodButton.onClick.AddListener(
            () => Event.Instance.EventTrigger("OnClickSubtractBloodButton")
            );

        m_AddBloodButton.onClick.AddListener(
            () => Event.Instance.EventTrigger("OnClickAddBloodButton")
            );

        //监听刷新血量事件
        Event.Instance.AddEventListener("RefreshBloodVolumeText", (int bloodVolume) => RefreshBloodVolumeText(bloodVolume));

        //发送创建Panel完成事件
        Event.Instance.EventTrigger("OnInstanceMainPanel");
    }
    #endregion

    #region 修改界面函数
    /// <summary>
    /// 刷新血量显示
    /// </summary>
    /// <param name="bloodVolume"></param>
    private void RefreshBloodVolumeText(int bloodVolume)
    {
        m_BloodVolumeText.text = "血量:" + bloodVolume;
    }
    #endregion
}

可以看到View层的代码很简洁,后续也应该只写UI相关的逻辑在里面,不要把逻辑写到这边来了哦。
这里将事件系统代码也放过来,大体上跟之前一样,略改了一些空判断:

using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 定义一个空接口,作为EventInfo的基类(使用基类存储子类),则字典中存储的为EventInfo类型
/// </summary>
public interface IEventInfo { }

/// <summary>
/// 将Action转化为泛型,包裹在EventInfo,则字典中存储的为EventInfo类型
/// </summary>
/// <typeparam name="T">T</typeparam>
public class EventInfo<T> : IEventInfo
{
    public Action<T> actions;
    public EventInfo(Action<T> action)
    {
        actions += action;
    }
}

/// <summary>
/// 用于不需要指定泛型的事件使用
/// </summary>
public class EventInfo : IEventInfo
{
    public Action actions;
    public EventInfo(Action action)
    {
        actions += action;
    }
}

/// <summary>
/// 事件中心
/// </summary>
public class Event : Singleton<Event>
{
    //用于储存所有事件的Dictionary
    private Dictionary<string, IEventInfo> m_EventDictionary = new Dictionary<string, IEventInfo>();

    /// <summary>
    /// 添加监听事件
    /// </summary>
    /// <param name="name"></param>
    /// <param name="action"></param>
    public void AddEventListener(string name, Action action)
    {
        if (m_EventDictionary.ContainsKey(name))
        {
            if (null == (m_EventDictionary[name] as EventInfo))
                Debug.LogError("添加了不同参数的委托");
            else
                (m_EventDictionary[name] as EventInfo).actions += action;
        }
        else
            m_EventDictionary.Add(name, new EventInfo(action));
    }

    /// <summary>
    /// 添加监听事件(有参)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="name">事件名</param>
    /// <param name="action">带参数的委托</param>
    public void AddEventListener<T>(string name, Action<T> action)
    {
        if (m_EventDictionary.ContainsKey(name))
        {
            if (null == (m_EventDictionary[name] as EventInfo<T>))
                Debug.LogError("添加了不同参数的委托");
            else
                (m_EventDictionary[name] as EventInfo<T>).actions += action;
        }
        else
            m_EventDictionary.Add(name, new EventInfo<T>(action));
    }

    /// <summary>
    /// 移除监听
    /// </summary>
    /// <param name="name"></param>
    /// <param name="action"></param>
    public void RemoveEventListener(string name, Action action)//移除
    {
        if (m_EventDictionary.ContainsKey(name))
        {
            (m_EventDictionary[name] as EventInfo).actions -= action;
            if (null == m_EventDictionary[name])
                m_EventDictionary.Remove(name);
        }
    }

    /// <summary>
    /// 移除监听(有参)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="name">事件名</param>
    /// <param name="action"></param>
    public void RemoveEventListener<T>(string name, Action<T> action)
    {
        if (m_EventDictionary.ContainsKey(name))
        {
            (m_EventDictionary[name] as EventInfo<T>).actions -= action;
            if (null == m_EventDictionary[name])
                m_EventDictionary.Remove(name);
        }
    }

    /// <summary>
    /// 触发事件
    /// </summary>
    /// <param name="name"></param>
    public void EventTrigger(string name)
    {
        if (!m_EventDictionary.ContainsKey(name))
        {
            Debug.Log("当前事件未被监听:" + name);
            return;
        }

        if (null == (m_EventDictionary[name] as EventInfo))
        {
            Debug.LogError("调用了不同参数的委托");
        }
        else if ((m_EventDictionary[name] as EventInfo).actions != null)
            (m_EventDictionary[name] as EventInfo).actions.Invoke();
    }

    /// <summary>
    /// 触发事件(有参)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="name"></param>
    /// <param name="info"></param>
    public void EventTrigger<T>(string name, T info)
    {
        if (!m_EventDictionary.ContainsKey(name))
        {
            Debug.Log("当前事件未被监听:" + name);
            return;
        }

        if (null == (m_EventDictionary[name] as EventInfo<T>))
        {
            Debug.LogError("调用了不同参数的委托");
        }
        else if ((m_EventDictionary[name] as EventInfo<T>).actions != null)
            (m_EventDictionary[name] as EventInfo<T>).actions.Invoke(info);
    }

    /// <summary>
    /// 清空所有事件
    /// </summary>
    public void Clear()
    {
        m_EventDictionary.Clear();
    }

    /// <summary>
    /// 删除事件及其所有监听
    /// </summary>
    /// <param name="name">事件名</param>
    public void DeleteEvent(string name)
    {
        if (m_EventDictionary.ContainsKey(name))
        {
            m_EventDictionary.Remove(name);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值