標籤:unity event delegate handle
猴子原創,歡迎轉載。轉載請註明: 轉載自Cocos2Der-CSDN,謝謝!
原文地址: http://blog.csdn.net/cocos2der/article/details/46539433
Delegate作用我就不多說了,Unity中可以直接使用EventHandler實現事件委託,咱們直接案例吧。
一、情境物體移動結束後事件監聽
假如PlayerControl,移動結束後觸發MoveComplete事件。
using UnityEngine;using System.Collections;using System;public class PlayerControl : MonoBehaviour { public event EventHandler MoveComplete; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonUp(0)) { // Test logic for PlayerMoveComplete PlayerMoveComplete(); } } void PlayerMoveComplete() { if (MoveComplete != null) { MoveComplete(this, EventArgs.Empty); } }}
事件接收處理
using UnityEngine;using System.Collections;using System;public class GameManager : MonoBehaviour { public static GameManager Instance; public PlayerControl playerControl; void Awake () { // check there isn‘t more than one instance of the GameManager in the scene if(Instance != null){ Debug.LogError("More than one GameManager found in the scene"); return; } // set the global instance Instance = this; } // Use this for initialization void Start () { playerControl.MoveComplete += HandleMoveComplete; } void HandleMoveComplete (object sender, EventArgs e) { Debug.Log("MoveComplete:"); } // Update is called once per frame void Update () { }}
這裡由於使用的EventHandler實現,所以在事件中無法傳遞自訂參數。
二、自訂Event,事件中傳遞自訂參數
1、自訂EventArgs
using UnityEngine;using System.Collections;using System;public class PlayerMoveEventArgs : EventArgs { private string message; public PlayerMoveEventArgs(string message) { this.message = message; } public string Message { get{return message;} }}public delegate void MoveCompleteHandle(object sender, PlayerMoveEventArgs e);
那麼我們修改下PlayerControl
using UnityEngine;using System.Collections;using System;public class PlayerControl : MonoBehaviour { public event EventHandler MoveComplete; public event MoveCompleteHandle CustomMoveComplete; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonUp(0)) { // Test logic for PlayerMoveComplete PlayerMoveComplete(); } } void PlayerMoveComplete() { if (MoveComplete != null) { MoveComplete(this, EventArgs.Empty); } if (CustomMoveComplete != null) { CustomMoveComplete(this, new PlayerMoveEventArgs("Move:" + this.name)); } }}
處理事件的我們也修改下
using UnityEngine;using System.Collections;using System;public class GameManager : MonoBehaviour { public static GameManager Instance; public PlayerControl playerControl; void Awake () { // check there isn‘t more than one instance of the GameManager in the scene if(Instance != null){ Debug.LogError("More than one GameManager found in the scene"); return; } // set the global instance Instance = this; } // Use this for initialization void Start () { playerControl.MoveComplete += HandleMoveComplete; playerControl.CustomMoveComplete += HandleCustomMoveComplete; } void HandleCustomMoveComplete (object sender, PlayerMoveEventArgs e) { Debug.Log("HandleCustomMoveComplete:" + e.Message); } void HandleMoveComplete (object sender, EventArgs e) { Debug.Log("MoveComplete:"); } // Update is called once per frame void Update () { }}
ok,現在你可以自由的玩耍了。
事件/委託機制(event/delegate)(Unity3D開發之十七)