Event/delegate mechanism (Unity3D development 17th), delegateunity3d
Monkey original, reprinted. Reprinted Please note: Reprinted from the Cocos2Der-CSDN, thank you!
Address: http://blog.csdn.net/cocos2der/article/details/46539433
I will not talk about the role of Delegate. in Unity, you can directly use EventHandler to implement event delegation. Let's look at it directly.
I. event monitoring after scene object movement ends
If PlayerControl is used, the MoveComplete event is triggered after the move is completed.
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); } }}
Event receipt and processing
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 () { }}
Because the EventHandler implementation is used, custom parameters cannot be passed in the event.
Ii. Custom Event: Passing custom parameters in the Event
1. Customize 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);
Modify 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)); } }}
We have also modified the event processing method.
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. Now you can play freely.