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

这里写图片描述
这里写图片描述

using UnityEngine;
using System.Collections;

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

    /// 
    /// 消息优先级
    /// 
    /// 
    int EventPriority();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

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

    /// 
    /// 所有消息集合
    /// 
    private Dictionary> mListeners = new Dictionary>();

    /// 
    /// 消息节点
    /// 
    private List mNodeList = new List();

    /// 
    /// 挂载一个消息节点到当前节点上
    /// 
    /// 消息节点
    /// 如果当前节点里面已经包含要添加的这个节点那么返回false
    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;
    }

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

    /// 
    /// 挂载一个消息监听器到当前的消息节点
    /// 
    /// 消息ID
    /// 消息监听器
    /// 当前消息节点已经挂载了这个消息监听器那么返回false
    public bool AttachEventListener(int key,IEventListener listener)
    {
        if (listener == null)
        {
            return false;
        }
        if (!mListeners.ContainsKey(key))
        {
            mListeners.Add(key,new List() { 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;
    }

    /// 
    /// 卸载一个消息节点
    /// 
    /// 如果当前消息节点不存在那么返回false
    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);
    }

    /// 
    /// 派发消息到子消息节点以及自己节点下的监听器上
    /// 
    /// 消息ID
    /// 
    /// 
    /// 如果中断消息返回true
    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);
    }


    /// 
    /// 消息触发
    /// 
    /// 消息id
    /// 
    /// 
    /// 是否中断
    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 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();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169

测试:


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;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

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);
        }
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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, "测试消息发送");
        }
    }
}

你可能感兴趣的:(Unity3D游戏开发框架-消息机制)