前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >U3D观察者模式,实现1对多

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

作者头像
立羽
发布2023-08-24 13:45:02
1600
发布2023-08-24 13:45:02
举报
文章被收录于专栏:Unity3d程序开发

观察者类

代码语言:javascript
复制
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);
    }

}

使用:

订阅消息与取消订阅消息

代码语言:javascript
复制
    void EventInit()
    {
        NotificationCenter.Get().ObjAddEventListener(KEventKey.m_evOpenInputTip, gameObject, OnEventOpenInputTip);
    }

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

    void OnEventOpenInputTip(Notification param)
    {

    }

发布消息

代码语言:javascript
复制
 NotificationCenter.Get().ObjDispatchEvent(KEventKey.m_evDown, 1);

注意的地方1

代码语言:javascript
复制
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。先保存一遍要处理的消息,因为有些委托是创建对象,如果对象初始化执行

代码语言:javascript
复制
    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

代码语言:javascript
复制
    void OnEvNext(Notification noti)
    {
        if (gameObject.activeSelf == true)
        ChallengeMgr.m_instance.OnNextRet(GetScore());
    }

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 观察者类
  • 使用:
  • 注意的地方1
  • 注意的地方2
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档