關於Unity的委託事件處理指令碼之間的互動
阿新 • • 發佈:2019-02-17
這裡可能需要前期準備一下c#的委託(delegate)和事件(event)方面的知識了。。。
不是很明白的,就各種谷歌吧~
需要準備二個繼承與MonoBehaviour的EventDispatcher類和EventListener類。
我這裡吧delegate就寫在EventDispatcher裡了,也可以新建一個類吧這些delegate存在裡面,方便一點。。。
然後重寫MonoBehaviour支援的那些時間響應函式:
比如OnMouseDown();
首先定義一個EventHandler 這個委託的事件。。。然後再OnMouseDown響應函式裡對他進行操作。。
如果某個GameObject偵聽了這個事件並寫出了響應函式(響應函式即是定義事件的委託delegate),那麼在重寫的OnMouseDown函式類就會呼叫執行這個傳進來的delegate並且把this.gameObject傳出去。
以此類推,附上EventDispatcher類:
首先需要獲取目標元件上的EventDispatcher元件。
完了再寫出處理函式。。。
附上EventListener原始碼:
完成了。。。
有問題,大家一起研究,一起學習,一起進步~~
不是很明白的,就各種谷歌吧~
需要準備二個繼承與MonoBehaviour的EventDispatcher類和EventListener類。
我這裡吧delegate就寫在EventDispatcher裡了,也可以新建一個類吧這些delegate存在裡面,方便一點。。。
- //定義委託事件,它定義了可以代表的方法的型別
- /**事件*/
- public delegate void EventHandler(GameObject e);
- /**碰撞檢測*/
- public delegate void CollisionHandler(GameObject e,Collision c);
- /**觸發器檢測*/
- public delegate void TriggerHandler(GameObject e,Collider other);
- /**控制器碰撞檢測*/
- public delegate void ControllerColliderHitHandler(GameObject e,ControllerColliderHit hit);
- /**新關卡被載入進來*/
- public delegate void LevelWasLoadedHandler(GameObject e,int level);
然後重寫MonoBehaviour支援的那些時間響應函式:
比如OnMouseDown();
首先定義一個EventHandler 這個委託的事件。。。然後再OnMouseDown響應函式裡對他進行操作。。
如果某個GameObject偵聽了這個事件並寫出了響應函式(響應函式即是定義事件的委託delegate),那麼在重寫的OnMouseDown函式類就會呼叫執行這個傳進來的delegate並且把this.gameObject傳出去。
- public event EventHandler MouseDown;
- void OnMouseDown(){
- if (MouseDown != null)
- MouseDown (this.gameObject);
- }
以此類推,附上EventDispatcher類:
- using System;
- using UnityEngine;
- /**
- * 基於MonoBehaviour的一個事件派發類
- * A simple event dispatcher - allows to listen to events in one GameObject from another GameObject
- *
- *
- * 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.
- *
- * date: 2013.8.21
- */
- public class EventDispatcher:MonoBehaviour{
- //定義委託事件,它定義了可以代表的方法的型別
- /**事件*/
- public delegate void EventHandler(GameObject e);
- /**碰撞檢測*/
- public delegate void CollisionHandler(GameObject e,Collision c);
- /**觸發器檢測*/
- public delegate void TriggerHandler(GameObject e,Collider other);
- /**控制器碰撞檢測*/
- public delegate void ControllerColliderHitHandler(GameObject e,ControllerColliderHit hit);
- /**新關卡被載入進來*/
- public delegate void LevelWasLoadedHandler(GameObject e,int level);
- public event EventHandler MouseEnter;
- void OnMouseEnter(){
- if (MouseEnter != null) //如果有方法註冊委託變數
- MouseEnter(this.gameObject); //通過委託呼叫方法
- }
- public event EventHandler MouseOver;
- void OnMouseOver(){
- if (MouseOver != null)
- MouseOver (this.gameObject);
- }
- public event EventHandler MouseExit;
- void OnMouseExit(){
- if (MouseExit != null)
- MouseExit (this.gameObject);
- }
- public event EventHandler MouseDown;
- void OnMouseDown(){
- if (MouseDown != null)
- MouseDown (this.gameObject);
- }
- public event EventHandler MouseUp;
- void OnMouseUp(){
- if (MouseUp != null)
- MouseUp (this.gameObject);
- }
- public event EventHandler MouseDrag;
- void OnMouseDrag(){
- if (MouseDrag != null)
- MouseDrag (this.gameObject);
- }
- /**當renderer(渲染器)在任何相機上可見時呼叫OnBecameVisible*/
- public event EventHandler BecameVisible;
- void OnBecameVisible (){
- if (BecameVisible != null)
- BecameVisible (this.gameObject);
- }
- /**當renderer(渲染器)在任何相機上都不可見時呼叫OnBecameInvisible*/
- public event EventHandler BecameInvisible;
- void OnBecameInvisible (){
- if (BecameInvisible != null)
- BecameInvisible (this.gameObject);
- }
- public event EventHandler Enable;
- void OnEnable(){
- if(Enable != null)
- Enable(this.gameObject);
- }
- public event EventHandler Disable;
- void OnDisable(){
- if(Disable != null)
- Disable(this.gameObject);
- }
- public event EventHandler Destroy;
- void OnDestroy(){
- if(Destroy != null)
- Destroy(this.gameObject);
- }
- /**在相機渲染場景之前呼叫*/
- public event EventHandler PreRender;
- void OnPreRender(){
- if(PreRender != null)
- PreRender(this.gameObject);
- }
- /**在相機完成場景渲染之後呼叫*/
- public event EventHandler PostRender;
- void OnPostRender(){
- if(PostRender != null)
- PostRender(this.gameObject);
- }
- /**在相機場景渲染完成後被呼叫*/
- public event EventHandler RenderObject;
- void OnRenderObject(){
- if(RenderObject != null)
- RenderObject(this.gameObject);
- }
- public event EventHandler ApplicationPause;
- void OnApplicationPause(){
- if(ApplicationPause != null)
- ApplicationPause(this.gameObject);
- }
- /**當玩家獲得或失去焦點時傳送給所有遊戲物體*/
- public event EventHandler ApplicationFocus;
- void OnApplicationFocus(){
- if(ApplicationFocus != null)
- ApplicationFocus(this.gameObject);
- }
- public event EventHandler ApplicationQuit;
- void OnApplicationQuit(){
- if(ApplicationQuit != null)
- ApplicationQuit(this.gameObject);
- }
- public event TriggerHandler TriggerEnter;
- void OnTriggerEnter(Collider other){
- if(TriggerEnter != null)
- TriggerEnter(this.gameObject,other);
- }
- public event TriggerHandler TriggerExit;
- void OnTriggerExit(Collider other){
- if(TriggerExit != null)
- TriggerExit(this.gameObject,other);
- }
- public event TriggerHandler TriggerStay;
- void OnTriggerStay(Collider other){
- if(TriggerStay != null)
- TriggerStay(this.gameObject,other);
- }
- public event ControllerColliderHitHandler controllerColliderHit;
- void OnControllerColliderHit(ControllerColliderHit hit){
- if(controllerColliderHit != null)
- controllerColliderHit(this.gameObject,hit);
- }
- public event CollisionHandler CollisionEnter;
- void OnCollisionEnter (Collision c){
- if (CollisionEnter != null)
- CollisionEnter (this.gameObject, c);
- }
- public event CollisionHandler CollisionStay;
- void OnCollisionStay (Collision c){
- if (CollisionStay != null)
- CollisionStay (this.gameObject, c);
- }
- public event CollisionHandler CollisionExit;
- void OnCollisionExit (Collision c){
- if (CollisionExit != null)
- CollisionExit (this.gameObject, c);
- }
- public event LevelWasLoadedHandler LevelWasLoaded;
- void OnLevelWasLoaded(int level){
- if(LevelWasLoaded != null)
- LevelWasLoaded(this.gameObject,level);
- }
- }
首先需要獲取目標元件上的EventDispatcher元件。
- private EventDispatcher evt = null;
- // Use this for initialization
- void Start () {
- evt = GameObject.Find("Cube1").GetComponent<EventDispatcher>();
- }
- /**註冊偵聽器函式*/
- void addEventListener(){
- //給委託的事件型別變數賦值
- evt.MouseDown +=OnMouseDownListener;
- evt.MouseDrag += OnMouseDragLIstener;
- }
完了再寫出處理函式。。。
- /**事件響應處理函式
- * @param GameObject e 事件源的GameObject物件
- * <li> 事件響應函式的引數,參見EventDispatcher類中的相應事件Handler的引數個數
- * */
- void OnMouseDownListener(GameObject e){
- print("on mouse down..");
- e.transform.Rotate(20, 0, 0);
- //移除偵聽函式
- e.GetComponent<EventDispatcher>().MouseDown -= OnMouseDownListener;
- }
- void OnMouseDragLIstener(GameObject e){
- print("on mouse drag..");
- }
附上EventListener原始碼:
- using UnityEngine;
- using System.Collections;
- /**
- * 響應事件
- * */
- public class EventListener : MonoBehaviour {
- private EventDispatcher evt = null;
- // Use this for initialization
- void Start () {
- evt = GameObject.Find("Cube1").GetComponent<EventDispatcher>();
- addEventListener();
- }
- /**註冊偵聽器函式*/
- void addEventListener(){
- //給委託的事件型別變數賦值
- evt.MouseDown +=OnMouseDownListener;
- evt.MouseDrag += OnMouseDragLIstener;
- }
- // Update is called once per frame
- void Update () {
- }
- /**事件響應處理函式
- * @param GameObject e 事件源的GameObject物件
- * <li> 事件響應函式的引數,參見EventDispatcher類中的相應事件Handler的引數個數
- * */
- void OnMouseDownListener(GameObject e){
- print("on mouse down..");
- e.transform.Rotate(20, 0, 0);
- //移除偵聽函式
- //e.GetComponent<EventDispatcher>().MouseDown -= OnMouseDownListener;
- }
- void OnMouseDragLIstener(GameObject e){
- print("on mouse drag..");
- }
- }
完成了。。。
有問題,大家一起研究,一起學習,一起進步~~