事件/委託機制(event/delegate)(Unity3D開發之十七)

來源:互聯網
上載者:User

標籤: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開發之十七)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.