U3D观察者模式,实现1对多

观察者类

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

public class Notification
{
    /// <summary>
    /// 通知发送者
    /// </summary>
    public GameObject sender;

    /// <summary>
    /// 通知内容
    /// 备注:在发送消息时需要装箱、解析消息时需要拆箱
    /// </summary>
    public object param;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="param"></param>
    public Notification(object param,GameObject sender = null)
    {
        this.sender = sender;
        this.param = param;
    }

    /// <summary>
    /// 实现ToString方法
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return string.Format("sender={0},param={1}", this.sender, this.param);
    }
}

public class NotificationCenter {

    /// <summary>
    /// 通知中心单例
    /// </summary>
    private static NotificationCenter instance = null;
    public delegate void OnNotification(Notification notific);
    public static NotificationCenter Get()
    {
        if (instance == null)
        {
            instance = new NotificationCenter();
            return instance;
        }
        return instance;
    }


    //实现一对多的消息
    private Dictionary<string, Dictionary<GameObject, OnNotification>> m_dicEvent
    = new Dictionary<string, Dictionary<GameObject, OnNotification>>();

    public void ObjAddEventListener(string eventKey, GameObject obj,OnNotification eventListener)
    {
        if (!m_dicEvent.ContainsKey(eventKey))
        {
            Dictionary<GameObject, OnNotification> dic = new Dictionary<GameObject, OnNotification>();
            dic[obj] = eventListener;
            m_dicEvent[eventKey] = dic;
        }
        else
        {
            m_dicEvent[eventKey][obj] = eventListener;
        }
    }

    public void ObjRemoveEventListener(string eventKey,GameObject obj)
    {
        if (!m_dicEvent.ContainsKey(eventKey))
            return;

        m_dicEvent[eventKey].Remove(obj);
    }


    public void ObjDispatchEvent(string eventKey, object param = null,GameObject sender = null)
    {
        if (!m_dicEvent.ContainsKey(eventKey))
            return;
        List<GameObject> listRemoveKey = new List<GameObject>();
        List<OnNotification> listNoti = new List<OnNotification>();
        lock (m_dicEvent)
        {
            foreach (var it in m_dicEvent[eventKey])
            {
                if (it.Key != null)
                {
                    //it.Value(new Notification(param, sender));
                    listNoti.Add(it.Value);
                }
                else
                {
                    listRemoveKey.Add(it.Key);
                }
            }

            //删除已经Destory的GameObject
            for (int i = 0; i < listRemoveKey.Count; i++)
            {
                m_dicEvent[eventKey].Remove(listRemoveKey[i]);
            }

            for ( int  i = 0; i < listNoti.Count; i++)
            {
                listNoti[i](new Notification(param, sender));
            }
        }
    }

    /// <summary>
    /// 是否存在指定事件的监听器
    /// </summary>
    public bool HasEventListener(string eventKey)
    {
        return m_dicEvent.ContainsKey(eventKey);
    }

}



使用:

订阅消息与取消订阅消息

    void EventInit()
    {
        NotificationCenter.Get().ObjAddEventListener(KEventKey.m_evOpenInputTip, gameObject, OnEventOpenInputTip);
    }

    private void OnDestroy()
    {
        NotificationCenter.Get().ObjRemoveEventListener(KEventKey.m_evOpenInputTip, gameObject);
    }

    void OnEventOpenInputTip(Notification param)
    {

    }

发布消息

 NotificationCenter.Get().ObjDispatchEvent(KEventKey.m_evDown, 1);

注意的地方1

lock (m_dicEvent)
        {
            foreach (var it in m_dicEvent[eventKey])
            {
                if (it.Key != null)
                {
                    //it.Value(new Notification(param, sender));
                    listNoti.Add(it.Value);
                }
                else
                {
                    listRemoveKey.Add(it.Key);
                }
            }

            //删除已经Destory的GameObject
            for (int i = 0; i < listRemoveKey.Count; i++)
            {
                m_dicEvent[eventKey].Remove(listRemoveKey[i]);
            }

            for ( int  i = 0; i < listNoti.Count; i++)
            {
                listNoti[i](new Notification(param, sender));
            }
        }

发出消息时,1.根据GameObject是否存在,删除对应消息表中。 2。先保存一遍要处理的消息,因为有些委托是创建对象,如果对象初始化执行

    public void Awake()
    {
        SetExamJianXiuDian();
        for (int i = 0; i < m_listId.Count; i++)
        {
            m_dicId[m_listId[i]] = false;
        }
        NotificationCenter.Get().ObjAddEventListener(KEventKey.m_evExamJianXiuDian, gameObject, OnEvJianXiuDian);

        NotificationCenter.Get().ObjAddEventListener(KEventKey.m_evChallengeCommit, gameObject, OnEvCommit);
        NotificationCenter.Get().ObjAddEventListener(KEventKey.m_evChallengeNext, gameObject, OnEvNext);
    }

就会产生执行委托,边遍历字典边修改字典问题

注意的地方2

    void OnEvNext(Notification noti)
    {
        if (gameObject.activeSelf == true)
        ChallengeMgr.m_instance.OnNextRet(GetScore());
    }

当执行委托时,要判断下这个脚本的GameObject是否激活状态,再去执行委托,不然出现一些难以确定bug

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值