unity 3d 使用C#的事件/委托机制
来源:互联网 发布:微信淘客发单软件 编辑:程序博客网 时间:2024/05/20 05:46
转自:http://blog.csdn.net/hiramtan/article/details/8955472
事件分发:
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); } }
0 0
- unity 3d 使用C#的事件/委托机制
- Unity 之C# 委托与事件机制
- C#委托/事件在Unity中的使用
- Unity&C#的委托事件总结
- c#委托 事件 机制
- C#委托事件的使用
- C#的GC机制,委托机制与Unity内存泄露
- 在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- C# 委托/事件在Unity中的使用(续)
- C#事件和委托机制
- C#委托和事件机制
- c#事件,委托机制(转)
- 在C#的事件、多播中使用委托
- C#事件与委托的关系【使用】
- C#中事件和委托的使用
- Unity-3D 委托Delegate
- Unity3D研究之在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信引入中间层
- Unity3D技巧在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- leetcode|80|Remove Duplicates from Sorted Array II
- poj 2828 Buy Tickets 万能的线段树大法。
- LeetCode:Pascal's Triangle II
- 【 随笔 】 JavaScript 图形库的流行度调查
- HDU 1231 最大连续子序列
- unity 3d 使用C#的事件/委托机制
- 代码大全第二版读书笔记 第二部分-创建高质量的代码 九、伪代码编程过程
- 工作日志--秦浩
- 关于C指针的学习总结
- 10-0. 说反话 (20)
- 浪潮记(7):转正
- 利用Opencv 3.0源码 + Visual Studio 2013 在window7环境下编译lib及dll
- NEFU117 素数个数的位数【素数定理】
- HDU 1559 最大子矩阵