Event/delegate mechanism (Unity3D development 17th), delegateunity3d

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.