event delegate

using System;

namespace ConsoleApp1
{
    delegate void StudentDelegate();
    delegate T Mydele<T>(T a, T b);
    class Program
    {
        static void Main(string[] args)
        {
           
            EventFunction eventFunction = new EventFunction();
            InvokeDefine invokeDefine = new InvokeDefine();
            invokeDefine.StudentEvent += InvokeDefine_StudentEvent;
            invokeDefine.StudentEvent += eventFunction.Student1;
            invokeDefine.StudentEvent += eventFunction.Student2;

           invokeDefine.Invoke();         


            
        }
        private static void InvokeDefine_StudentEvent()
        {
            Console.WriteLine("Hello World!"); ;
        }
    }

    /// <summary>
    ///定义事件和调用事件一定要放在一个类里面
    /// </summary>
    class InvokeDefine
    {
        public event StudentDelegate StudentEvent;
        public void Invoke()
        {
       
            StudentEvent?.Invoke();//?null检杳运算符同上
        }
  
    }

    class EventFunction
    {
        public void Student1()
        {
            Console.WriteLine("我是Ant001");
        }
        public void Student2()
        {
            Console.WriteLine("我是Ant002");
        }
    }
   
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public enum EventType { None, ButtonPress, KeyPress, MouseClick } public class EventManager : MonoBehaviour { public delegate void EventDelegate(EventType eventType); private Dictionary<EventType, EventDelegate> eventDictionary; private static EventManager eventManager; public static EventManager instance { get { if (!eventManager) { eventManager = FindObjectOfType(typeof(EventManager)) as EventManager; if (!eventManager) { Debug.LogError("There needs to be one active EventManager script on a GameObject in your scene."); } else { eventManager.Init(); } } return eventManager; } } void Init() { if (eventDictionary == null) { eventDictionary = new Dictionary<EventType, EventDelegate>(); } } public static void StartListening(EventType eventType, EventDelegate listener) { EventDelegate thisEvent; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent += listener; instance.eventDictionary[eventType] = thisEvent; } else { thisEvent += listener; instance.eventDictionary.Add(eventType, thisEvent); } } public static void StopListening(EventType eventType, EventDelegate listener) { if (eventManager == null) return; EventDelegate thisEvent; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent -= listener; instance.eventDictionary[eventType] = thisEvent; } } public static void TriggerEvent(EventType eventType) { EventDelegate thisEvent = null; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent.Invoke(eventType); } } }添加注释
06-08
// 定义事件类型枚举 public enum EventType { None, // 无事件类型 ButtonPress, // 按钮点击事件 KeyPress, // 按键按下事件 MouseClick // 鼠标点击事件 } public class EventManager : MonoBehaviour { // 定义事件委托 public delegate void EventDelegate(EventType eventType); // 事件类型与事件委托的字典 private Dictionary<EventType, EventDelegate> eventDictionary; // 静态的事件管理器实例 private static EventManager eventManager; // 获取事件管理器实例的属性 public static EventManager instance { get { // 如果事件管理器实例不存在,则查找场景中是否存在 EventManager 脚本 if (!eventManager) { eventManager = FindObjectOfType(typeof(EventManager)) as EventManager; // 如果不存在,则输出错误信息 if (!eventManager) { Debug.LogError("There needs to be one active EventManager script on a GameObject in your scene."); } // 如果存在,则初始化事件字典 else { eventManager.Init(); } } return eventManager; } } // 初始化事件字典 void Init() { if (eventDictionary == null) { eventDictionary = new Dictionary<EventType, EventDelegate>(); } } // 添加事件监听器 public static void StartListening(EventType eventType, EventDelegate listener) { EventDelegate thisEvent; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent += listener; instance.eventDictionary[eventType] = thisEvent; } else { thisEvent += listener; instance.eventDictionary.Add(eventType, thisEvent); } } // 移除事件监听器 public static void StopListening(EventType eventType, EventDelegate listener) { if (eventManager == null) return; EventDelegate thisEvent; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent -= listener; instance.eventDictionary[eventType] = thisEvent; } } // 触发事件 public static void TriggerEvent(EventType eventType) { EventDelegate thisEvent = null; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent.Invoke(eventType); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值