事件/委托机制(event/delegate)(Unity3D开发之十七)

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址:

Delegate作用我就不多说了,,Unity中可以直接使用EventHandler实现事件委托,咱们直接事例吧。

一、场景物体移动结束后事件监听

假如PlayerControl,移动结束后触发MoveComplete事件。

using UnityEngine;using System.Collections;using System;public class PlayerControl : MonoBehaviour {public event EventHandler MoveComplete;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {if (Input.GetMouseButtonUp(0)) {// Test logic for PlayerMoveCompletePlayerMoveComplete();}}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 sceneif(Instance != null){Debug.LogError(“More than one GameManager found in the scene”);return;}// set the global instanceInstance = this;}// Use this for initializationvoid Start () {playerControl.MoveComplete += HandleMoveComplete;}void HandleMoveComplete (object sender, EventArgs e){Debug.Log(“MoveComplete:”);}// Update is called once per framevoid 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;}}}(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 initializationvoid Start () {}// Update is called once per framevoid Update () {if (Input.GetMouseButtonUp(0)) {// Test logic for PlayerMoveCompletePlayerMoveComplete();}}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 sceneif(Instance != null){Debug.LogError(“More than one GameManager found in the scene”);return;}// set the global instanceInstance = this;}// Use this for initializationvoid 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 framevoid Update () {}}

ok,现在你可以自由的玩耍了。

不要害怕错过什么,因为在路上你就已经收获了自由自在的好心情。

事件/委托机制(event/delegate)(Unity3D开发之十七)

相关文章:

你感兴趣的文章:

标签云: