9288人加入学习
(57人评价)
Unity高度解耦和 - 事件的监听与广播系统(Unity2018.1.0)

制作完成于2018年11月24日

价格 免费

1  button 上挂载脚本 

拿到text 物体设置 text

 

 

游戏里面 物体尽量少挂载脚本

单例模式  使用频繁 耦合性高

public static ShowText  Instance

 

每个代码只需要 负责各自的任务  

[展开全文]

当需要调用方法时,将该方法做成一个监听,AddListener(事件码,方法)委托需要与事件码绑定。

方法若持有参数,在AddListener前指定同类型的泛型。

添加完监听后要记得remove

监听要在初始化函数(Awake、Start)中调用

外界想要调用方法时只需要广播事件码即可,达到解耦合目的

[展开全文]

监听

Awake()/Start()

{

EventCenter.AddListener<Type>(EventType.Name, 方法);

}

OnDestroy()

{

EventCenter.RemoveListener<Type>(EventType.Name, 方法);

}

[展开全文]

private void Awake()

{

    gameObject.SetActive(false);

    EventCenter.AddListener<string>(EventType.ShowText,Show);

}

private void OnDestroy()

{

    EventCenter.RemoveListener<string>(EventType.ShowText, Show);

}

private void Show(shring str)

{

    gameObject.SetActive(true);

    GetComponent<Text>().text = str;

}

[展开全文]

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

public class EventCenter {
    private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();

    //注册监听事件
    public static void AddListener<T>(EventType eventType, CallBack<T> callBack) {
        if (!m_EventTable.ContainsKey(eventType)) {
            m_EventTable.Add(eventType, null);
        }
        Delegate d = m_EventTable[eventType];
        if (d != null && d.GetType() != callBack.GetType()) {
            throw new Exception(string.Format("添加监听错误:当前尝试为事件类型{0}添加不同的委托,原本的委托{1},现要添加的委托是{2}",
                eventType, d.GetType(),callBack.GetType
                ()));
        }
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }
    //移除监听事件
    public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack) {
        if (m_EventTable.ContainsKey(eventType))
        {
            Delegate d = m_EventTable[eventType];
            if (d == null)
            {
                throw new Exception(string.Format("移除监听错误:事件{0}不存在委托", eventType));

            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移动监听错误:尝试为事件{0}移除不同的委托,原先的委托为{1},现在要移除的委托为{2}"
                    , eventType, d.GetType(), callBack.GetType()));

            }
        }
        else {
            throw new Exception(string.Format("移除监听错误:不存在事件{0}", eventType
                ));


        }
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
        if (m_EventTable[eventType] == null) {
            m_EventTable.Remove(eventType);

        }
    }
    //广播事件
    public static void Broadcast<T>(EventType eventType, T arg) {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d)) {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);

            }
            else {
                throw new Exception(string.Format("事件广播错误:事件{0}存在不同的委托类型", eventType));
            }

        }
    }

}

[展开全文]

耦合性:是指代码之间的必然关系。不能缺少的

委托:

时间的监听:

显示文本的三种方法

1,Text.text="欢迎你";

2.单例模式

text:

public static showText Instance;

private void Awake(){

Instaance =this;

}

public void Show(String str){

GetComponent<Text>().text=str;

}

点击按钮后

ShowText.Instance.Show("欢迎你");

 

3.监听与广播

private void Awake(){

gameObject.SetActive(false);

EventCenter.AddListener<string>(EventType.ShowText,Show);//将方法做成监听

}

private void OnDestroy(){

EventCenter.RemoveListener<string>(EventType.ShowText,show);//移除监听

}

private void Show(string str){

gameObject.SetActive(true);

GetComponent<Text>().text=str;

}

广播

EventCenter.Broadcast(EventType.ShowText,"欢迎你");

 

[展开全文]

EventCenter.AddListener<string>(事件,方法);

1.初始化(Awake)里调用。

2.传递相应的类型。

 

EventCenter.Broadcast(事件,广播内容);

 

结论:解除按键和按钮的耦合性。

[展开全文]