unity 事件(委托)
来源:互联网 发布:ubuntu怎么安装jdk 编辑:程序博客网 时间:2024/05/02 01:54
头段时间做NGUI的时候,老大给我优化了很多,用到了C#的事件。由于我之前不是学C#的,下来花了点时间看了一下事件。我老大主要把事件用于对UI界面的切换。下面我们来看看代码吧。我的例子很简单的。
EventManager.cs
- using System;
- using System.Collections.Generic;
- public class EventArgs
- {
- }
- public class EventManager
- {
- static EventManager _instance;
- public static EventManager Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new EventManager();
- }
- return _instance;
- }
- }
- public delegate void EventDelegate<T>(T e) where T : EventArgs;
- readonly Dictionary<Type, Delegate> _delegates = new Dictionary<Type, Delegate>();
- public void AddListener<T>(EventDelegate<T> listener) where T : EventArgs
- {
- Delegate d;
- if (_delegates.TryGetValue(typeof(T), out d))
- {
- _delegates[typeof(T)] = Delegate.Combine(d, listener);
- }
- else
- {
- _delegates[typeof(T)] = listener;
- }
- }
- public void RemoveListener<T>(EventDelegate<T> listener) where T : EventArgs
- {
- Delegate d;
- if (_delegates.TryGetValue(typeof(T), out d))
- {
- Delegate currentDel = Delegate.Remove(d, listener);
- if (currentDel == null)
- {
- _delegates.Remove(typeof(T));
- }
- else
- {
- _delegates[typeof(T)] = currentDel;
- }
- }
- }
- public void Raise<T>(T e) where T : EventArgs
- {
- if (e == null)
- {
- throw new ArgumentNullException("e");
- }
- Delegate d;
- if (_delegates.TryGetValue(typeof(T), out d))
- {
- EventDelegate<T> callback = d as EventDelegate<T>;
- if (callback != null)
- {
- callback(e);
- }
- }
- }
- }
EventArgs.cs
- using UnityEngine;
- using System.Collections;
- public class UIEventArgs : EventArgs {
- private bool _isOpen;
- public bool IsOpen
- {
- get
- {
- return _isOpen;
- }
- set
- {
- _isOpen = value;
- }
- }
- }
EventListener.cs(绑定在一个物体上)
- using UnityEngine;
- using System.Collections;
- public class EventListener : MonoBehaviour {
- // Use this for initialization
- void Start () {
- EventManager.Instance.AddListener<UIEventArgs>(OnReceive);
- }
- void OnReceive(UIEventArgs e)
- {
- if(e.IsOpen)
- {
- print ("Name:"+gameObject.name);
- }
- }
- }
SendEventMeg.cs (绑定在一个物体上)
- using UnityEngine;
- using System.Collections;
- public class SendEventMeg : MonoBehaviour {
- // Use this for initialization
- void Start () {
- }
- void Send ()
- {
- var arg = new UIEventArgs()
- {
- IsOpen = true,
- };
- EventManager.Instance.Raise(arg);
- }
- void OnGUI()
- {
- if(GUILayout.Button("Send"))
- {
- Send();
- }
- }
- }
结果:打印出“Name:xxxxx”.
- unity 事件(委托)
- unity 事件(委托)
- Unity委托事件
- unity delegate事件委托
- Unity 委托事件
- Unity中的委托和事件
- 委托事件模拟unity生命周期
- 【Unity经验】委托与事件
- unity 委托事件简单应用
- 【unity 代码升华篇】委托、事件全解析(一)
- 【unity 代码升华篇】委托、事件全解析(一)
- 【unity 代码升华篇】委托、事件全解析(一)
- C# 委托/事件在Unity中的使用(续)
- 【unity】【Code】【Event事件】通用代码库(三)——Unity事件与委托
- Unity 之C# 委托与事件机制
- unity C#中的委托和事件
- unity C# 中的委托和事件
- 详解Unity中的委托与事件
- PythonChallenge 挑战之路 Level-6
- Webservice_21_SOAP的基于契约优先开发用户管理_实现Wsdl
- 数组的顺序表示和实现
- 文件操作
- Maya: 菜单 编辑NURBS >曲面相交
- unity 事件(委托)
- 2013华为校园招聘机试题1
- ext2文件预分配
- 对数据库所有表名的读取
- WinCE程序C/C++/C#实现带时间标记的日志记录
- 文档与串行化
- 二叉树的顺序存储
- WPF中根据元素名称查找子元素
- FPGA AD0809 Verilog implement