unity 3d 使用C#的事件/委托机制

转自: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值