关闭

Unity3D游戏开发框架-消息机制

标签: 框架unity3d
2137人阅读 评论(0) 收藏 举报
分类:

转载请注明原文链接:http://blog.csdn.net/u013108312/article/details/52351826
这里写图片描述
这里写图片描述

using UnityEngine;
using System.Collections;

/// <summary>
/// 消息监听接口
/// </summary>
public interface IEventListener   
{
    /// <summary>
    /// 处理消息
    /// </summary>
    /// <param name="id">消息Id</param>
    /// <param name="param1">参数1</param>
    /// <param name="param2">参数2</param>
    /// <returns>是否终止消息派发</returns>
    bool HandleEvent(int id, object param1, object param2);

    /// <summary>
    /// 消息优先级
    /// </summary>
    /// <returns></returns>
    int EventPriority();
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EventNode : MonoBehaviour
{
    /// <summary>
    /// 节点优先级
    /// </summary>
    public int EventNodePriority { set; get; }

    /// <summary>
    /// 所有消息集合
    /// </summary>
    private Dictionary<int, List<IEventListener>> mListeners = new Dictionary<int, List<IEventListener>>();

    /// <summary>
    /// 消息节点
    /// </summary>
    private List<EventNode> mNodeList = new List<EventNode>();

    /// <summary>
    /// 挂载一个消息节点到当前节点上
    /// </summary>
    /// <param name="node">消息节点</param>
    /// <returns>如果当前节点里面已经包含要添加的这个节点那么返回false</returns>
    public bool AttachEventNode(EventNode node)
    {
        if (node == null)
        {
            return false;
        }

        if (mNodeList.Contains(node))
        {
            return false;
        }
        int pos = 0;
        for (int i = 0; i < mNodeList.Count;i++ )
        {
            if (node.EventNodePriority > mNodeList[i].EventNodePriority)
            {
                break;
            }
            pos++;
        }

        mNodeList.Insert(pos,node);
        return true;
    }

    /// <summary>
    /// 卸载一个消息节点
    /// </summary>
    /// <param name="node">消息节点</param>
    /// <returns>如果节点不存在那么返回false</returns>
    public bool DetachEventNode(EventNode node)
    {
        if (!mNodeList.Contains(node))
        {
            return false;
        }
        mNodeList.Remove(node);
        return true;
    }

    /// <summary>
    /// 挂载一个消息监听器到当前的消息节点
    /// </summary>
    /// <param name="key">消息ID</param>
    /// <param name="listener">消息监听器</param>
    /// <returns>当前消息节点已经挂载了这个消息监听器那么返回false</returns>
    public bool AttachEventListener(int key,IEventListener listener)
    {
        if (listener == null)
        {
            return false;
        }
        if (!mListeners.ContainsKey(key))
        {
            mListeners.Add(key,new List<IEventListener>() { listener });
            return true;
        }
        if (mListeners[key].Contains(listener))
        {
            return false;
        }
        int pos = 0;
        for (int i = 0;i< mListeners[key].Count;i++ )
        {
            if (listener.EventPriority() > mListeners[key][i].EventPriority())
            {
                break;
            }
            pos++;
        }
        mListeners[key].Insert(pos,listener);
        return true;
    }

    /// <summary>
    /// 卸载一个消息节点
    /// </summary>
    /// <returns>如果当前消息节点不存在那么返回false</returns>
    public bool DetachEventListener(int key,IEventListener listener)
    {
       if (mListeners.ContainsKey(key) && mListeners[key].Contains(listener))
       {
           mListeners[key].Remove(listener);
           return true;
       }
       return false;
    }

    public void SendEvent(int key,object param1 = null,object param2 = null)
    {
        DispatchEvent(key, param1, param2);
    }

    /// <summary>
    /// 派发消息到子消息节点以及自己节点下的监听器上
    /// </summary>
    /// <param name="key">消息ID</param>
    /// <param name="param1"></param>
    /// <param name="param2"></param>
    /// <returns>如果中断消息返回true</returns>
    private bool DispatchEvent(int key,object param1,object param2)
    {
        for (int i = 0; i < mNodeList.Count;i++ )
        {
            if (mNodeList[i].DispatchEvent(key, param1, param2)) return true;
        }
        return TriggerEvent(key, param1, param2);
    }


    /// <summary>
    /// 消息触发
    /// </summary>
    /// <param name="key">消息id</param>
    /// <param name="param1"></param>
    /// <param name="param2"></param>
    /// <returns>是否中断</returns>
    private bool TriggerEvent(int key,object param1,object param2)
    {
        if (!this.gameObject.activeSelf || !this.gameObject.activeInHierarchy || !this.enabled)
        {
            return false;
        }

        if (!mListeners.ContainsKey(key))
        {
            return false;
        }
        List<IEventListener> listeners = mListeners[key];
        for (int i = 0; i < listeners.Count; i++)
        {
            if (listeners[i].HandleEvent(key, param1, param2)) return true;
        }
        return false;
    }

   void OnApplicationQuit()
    {
        mListeners.Clear();
        mNodeList.Clear();
    }

}

测试:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class EventDef
{
    public const int EventTest1 = 1;
    public const int EventTest2 = EventTest1 + 1;


    public const int ResLoadFinish = 1000;

}

using UnityEngine;
using System.Collections;

public class EventListener : MonoBehaviour,IEventListener 
{
    // Awake is called when the script instance is being loaded.
    void Awake() 
    {
        if (EventNodeCore.Instance)
        {
            EventNodeCore.Instance.AttachEventListener(EventDef.EventTest1, this);
        }
    }

    void OnDestroy()
    {
        if (EventNodeCore.Instance)
        {
            EventNodeCore.Instance.DetachEventListener(EventDef.EventTest1, this);
        }
    }
    public bool HandleEvent(int id, object param1, object param2)
    {
        Debug.Log("EventListener.HandleEvent =>" + " id=" + id + "param1=" + param1);
        switch(id)
        {
            case EventDef.EventTest1:
                Debug.Log(this.name + "=>" + "HandleEvent EventTest1");
                return false;
        }
        return false;
    }

    public int EventPriority()
    {
        return 0;
    }
}

using UnityEngine;
using System.Collections;

public class EventListener1 : MonoBehaviour , IEventListener
{
    // Awake is called when the script instance is being loaded.
    void Awake() 
    {
        if (EventNode1.Instance)
        {
            EventNode1.Instance.AttachEventListener(EventDef.EventTest1, this);
            EventNode1.Instance.AttachEventListener(EventDef.EventTest2, this);
        }
    }
    void OnDestroy()
    {
        if (EventNode1.Instance)
        {
            EventNode1.Instance.DetachEventListener(EventDef.EventTest1, this);
            EventNode1.Instance.DetachEventListener(EventDef.EventTest2, this);
        }
    }
    // Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
    void Start()
    {
    }

    // Update is called every frame, if the MonoBehaviour is enabled.
    void Update() 
    {
    }

    public bool HandleEvent(int id, object param1, object param2)
    {
        Debug.Log("EventListener1." + "HandleEvent => id =" + id + " param1=" + param1);
        //return true 消息中断
        return false;
    }

    public int EventPriority()
    {
        return 1;
    }
}

using UnityEngine;
using System.Collections;

public class EventListener2 : MonoBehaviour,IEventListener 
{
    // Awake is called when the script instance is being loaded.
    void Awake() 
    {
        if (EventNode2.Instance)
        {
            EventNode2.Instance.AttachEventListener(EventDef.EventTest1, this);
        }
    }

    void OnDestroy()
    {
        if (EventNode2.Instance)
        {
            EventNode2.Instance.DetachEventListener(EventDef.EventTest1, this);
        }
    }

    public bool HandleEvent(int id, object param1, object param2)
    {
        Debug.Log("EventListener2.HandleEvent => id =" + id + " param1=" + param1);
        return false;
    }

    public int EventPriority()
    {
        return 2;
    }
}

using UnityEngine;
using System.Collections;

public class EventNode1 : EventNode
{
    private static EventNode1 mInstance;
    public static EventNode1 Instance
    {
        get
        {
            return mInstance;
        }
    }

    void Awake()
    {
        mInstance = this;

        //base.EventNodePriority = 30;

        if (EventNodeCore.Instance)
        {
            EventNodeCore.Instance.AttachEventNode(this);
        }
        Debug.Log("-------------1");
    }

    void OnDestroy()
    {
        if (EventNodeCore.Instance)
        {
            EventNodeCore.Instance.DetachEventNode(this);
        }
    }
}

using UnityEngine;
using System.Collections;

public class EventNode2 : EventNode 
{
    private static EventNode2 mInstance;
    public static EventNode2 Instance
    {
        get
        {
            return mInstance;
        }
    }

    // Awake is called when the script instance is being loaded.
    void Awake() 
    {
        mInstance = this;
        base.EventNodePriority = 20;
        if (EventNodeCore.Instance)
        {
            EventNodeCore.Instance.AttachEventNode(this);
        }
    }
    void OnDestroy()
    {
        if (EventNodeCore.Instance)
        {
            EventNodeCore.Instance.DetachEventNode(this);
        }
    }


}

using UnityEngine;
using System.Collections;

public class EventNodeCore : EventNode
{
    private static EventNodeCore mInstance;
    public static EventNodeCore Instance
    {
        get
        {
            return mInstance;
        }

    }
    void Awake()
    {
        mInstance = this;
        Debug.Log("-------------");
    }

    void OnDestroy()
    {

    }

    void OnGUI()
    {
        if (GUI.Button(new Rect (0,0,200,50),"EventNodecore.SenEvent"))
        {
            EventNodeCore.Instance.SendEvent(EventDef.EventTest1, "测试消息发送");
        }
        if (GUI.Button(new Rect(0, 60, 200, 50), "EventNode1.SenEvent"))
        {
            EventNode1.Instance.SendEvent(EventDef.EventTest1, "测试消息发送");
            EventNode1.Instance.SendEvent(EventDef.EventTest2, "测试消息发送");
        }
        if (GUI.Button(new Rect(0, 120, 200, 50), "EventNode2.SenEvent"))
        {
            EventNode2.Instance.SendEvent(EventDef.EventTest1, "测试消息发送");
        }
    }
}

转载请注明原文链接:http://blog.csdn.net/u013108312/article/details/52351826

1
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:115868次
    • 积分:2692
    • 等级:
    • 排名:第13378名
    • 原创:152篇
    • 转载:30篇
    • 译文:0篇
    • 评论:8条
    最新评论