Unity组件开发--事件管理器

1.创建单例脚本:SingletonBase

public class SingletonBase where T : new()
{
    private static T instance;
    // 多线程安全机制
    private static readonly object locker = new object();
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                //lock写第一个if里是因为只有该类的实例还没创建时,才需要加锁,这样可以节省性能
                lock (locker)
                {
                    if (instance == null)
                        instance = new T();
                }
            }
            return instance;
        }
    }
}

2.事件管理器单例脚本:EventManager 

using System.Collections.Generic;
using System;
using System.Diagnostics;
using UnityEngine.Device;
/// 
/// 便于触发事件的扩展类
/// 
public static class EventTriggerExt
{
    /// 
    /// 触发事件(无参数)
    /// 
    /// 触发源
    /// 事件名
    public static void TriggerEvent(this object sender, string eventName)
    {
        EventManager.Instance.TriggerEvent(eventName, sender,null);
    }
    /// 
    /// 触发事件(有参数)
    /// 
    /// 触发源
    /// 事件名
    /// 事件参数
    public static void TriggerEvent(this object sender, string eventName, EventArgs args)
    {
        EventManager.Instance.TriggerEvent(eventName, sender, args);
    }

}
/// 
/// 事件管理器
/// 
public class EventManager : SingletonBase
{
    private Dictionary handlerDic = new Dictionary();

    /// 
    /// 添加一个事件的监听者
    /// 
    /// 事件名
    /// 事件处理函数
    public void AddListener(string eventName, EventHandler handler)
    {
        if (handlerDic.ContainsKey(eventName))
            handlerDic[eventName] += handler;
        else
            handlerDic.Add(eventName, handler);
    }
    /// 
    /// 移除一个事件的监听者
    /// 
    /// 事件名
    /// 事件处理函数
    public void RemoveListener(string eventName, EventHandler handler)
    {
        if (handlerDic.ContainsKey(eventName))
            handlerDic[eventName] -= handler;
    }

    /// 
    /// 触发事件 无senser
    /// 
    /// 
    /// 
    public void TriggerEvent(string eventName, EventArgs args)
    {
        TriggerEvent(eventName, null, args);


    }
    /// 
    /// 触发事件(有参数)
    /// 
    /// 事件名
    /// 触发源
    /// 事件参数
    public void TriggerEvent(string eventName, object sender, EventArgs args)
    {
        if (!Application.isPlaying) {
            return;
        }
        UnityEngine.Debug.Log("TriggerEvent:"+ eventName);
#if UNITY_EDITOR
        if (handlerDic.ContainsKey(eventName))
            handlerDic[eventName]?.Invoke(sender, args);
#else
        try {
                if (handlerDic.ContainsKey(eventName))
                    handlerDic[eventName]?.Invoke(sender, args);
            }
            catch (Exception e) {
                UnityEngine.Debug.LogError(e.Message + "\n" + e.StackTrace);
            }
#endif


    }
    /// 
    /// 清空所有事件
    /// 
    public void Clear()
    {
        handlerDic.Clear();
    }
}

3.事件名称定义脚本:EventName

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

public static class EventName
{
    public const string SocketOpen = nameof(SocketOpen);
    
    
}

4.事件回调参数脚本:CustomEventArgs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UTNET;
using agora_gaming_rtc;
using UnityEngine.Events;
//将所有的事件参数类统一写在这个脚本



public class LightColorChangeEventArgs : EventArgs
{
    public float red;
    public float green;
    public float blue;
}

5.如何使用:

EventManager.Instance.AddListener(EventName.OnSceneLoaded, (s, e) => { //场景未加载之前,不让玩家操作
    enabled = true;
});
EventManager.Instance.AddListener(EventName.ChangeAngle, changeAngle);
this.TriggerEvent(EventName.ChangeAngle, new AngleChangeEventArgs { angleIndex = 3 });

你可能感兴趣的:(unity组件开发,unity,c#,游戏程序)