unity 3d 使用C#的事件/委托机制
2015-02-02 21:41
357 查看
转自:http://blog.csdn.net/hiramtan/article/details/8955472
事件分发:
事件监听:
结束监听某个事件:
一个通用的EventDispatcher类,实现所有GameObject能够分发的事件。
事件分发:
using UnityEngine; public class EventDispatcher : MonoBehaviour { public delegate void EventHandler(GameObject e); public event EventHandler MouseOver; void OnMouseOver () { if (MouseOver != null) MouseOver (this.gameObject); } }
事件监听:
private GameObject s; [...] s.GetComponent<EventDispatcher>().MouseOver += Listener; [...] void Listener(GameObject g) { // g is being hovered, do something... }
结束监听某个事件:
s.GetComponent<EventDispatcher>().MouseOver -= Listener;
一个通用的EventDispatcher类,实现所有GameObject能够分发的事件。
using UnityEngine; using System.Collections; /** * A simple event dispatcher - allows to listen to events in one GameObject from another GameObject * * Author: Bartek Drozdz (bartek [at] everyday3d [dot] com) * * Usage: * Add this script to the object that is supposed to dispatch events. * In another objects follow this pattern to register as listener at intercept events: void Start () { EventDispatcher ev = GameObject.Find("someObject").GetComponent<EventDispatcher>(); ev.MouseDown += ListeningFunction; // Register the listener (and experience the beauty of overloaded operators!) } void ListeningFunction (GameObject e) { e.transform.Rotate(20, 0, 0); // 'e' is the game object that dispatched the event e.GetComponent<EventDispatcher>().MouseDown -= ListeningFunction; // Remove the listener } * This class does not implement all standards events, nor does it allow dispatching custom events, * but you shold have no problem adding all the other methods. */ public class EventDispatcher : MonoBehaviour { public delegate void EventHandler (GameObject e); public delegate void CollisionHandler (GameObject e, Collision c); public event EventHandler MouseOver; void OnMouseOver () { if (MouseOver != null) MouseOver (this.gameObject); } public event EventHandler MouseDown; void OnMouseDown () { if (MouseDown != null) MouseDown (this.gameObject); } public event EventHandler MouseEnter; void OnMouseEnter () { if (MouseEnter != null) MouseEnter (this.gameObject); } public event EventHandler MouseExit; void OnMouseExit () { if (MouseExit != null) MouseExit (this.gameObject); } public event EventHandler BecameVisible; void OnBecameVisible () { if (BecameVisible != null) BecameVisible (this.gameObject); } public event EventHandler BecameInvisible; void OnBecameInvisible () { if (BecameInvisible != null) BecameInvisible (this.gameObject); } public event CollisionHandler CollisionEnter; void OnCollisionEnter (Collision c) { if (CollisionEnter != null) CollisionEnter (this.gameObject, c); } public event CollisionHandler CollisionExit; void OnCollisionExit (Collision c) { if (CollisionExit != null) CollisionExit (this.gameObject, c); } }
相关文章推荐
- 聊聊在博客园写博客的这两年&&《Unity 3D脚本编程:使用C#语言开发跨平台游戏》正式出版
- Tolua使用笔记六:在lua中操作C#的委托事件与在lua中对Unity的GameObject的操作
- Unity里使用包含C#事件的Dll
- 《Unity 3D脚本编程:使用C#语言开发跨平台游戏》序言
- [Unity&C#&委托事件&未实例化]使用委托事件的时候事件对象未实例化
- Tolua使用笔记六:在lua中操作C#的委托事件与在lua中对Unity的GameObject的操作
- Unity 3D: c#中字符串截取使用的方法
- 在C#中使用代理的方式触发事件 (委托和事件 )
- 在C#中使用代理的方式触发事件_转摘
- c#写的五子棋程序,供学习WinForms的鼠标事件和使用GDI+
- C# 中用户控件中添加事件和使用
- [导入]在C#中使用代理的方式触发事件
- 在C#中使用代理的方式触发事件
- 在C#中使用代理的方式触发事件
- 使用c#捕获windows的关机事件(转载)
- 使用c#捕获windows的关机事件
- 在C#中使用代理的方式触发事件_转摘
- 在C#的事件、多播中使用委托
- 一个程序帮助你熟悉和使用C#中委托和事件
- 在C#中使用代理的方式触发事件