unityEvent 含參數的使用

unity 內置的事件,unityevent 的 使用。最多使用四個參數。且參數類型有限制,只能使用簡單的Bool值,int值,string等類型,不支持自定義類的作爲形參。

using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using System;
public class EventTry : MonoBehaviour {

    public Events_Bool currentChanged;
    // Use this for initialization
  //  bool isc;
    void Start () {
       
       currentChanged.AddListener(isOK);
    }
    
    // Update is called once per frame
    void Update () {
    if(Input.GetKeyDown(KeyCode.S))
    {
      //  isc = true;
        currentChanged.Invoke(true);
    }
    if (Input.GetKeyUp(KeyCode.D))
    {
      //  isc = false;
        currentChanged.Invoke(false); 
    }
   
    }
   public  void isOK(bool I)
    {
        if (I)
        {
            print("is Ok");
        }
        else
        {
            print("not is ok");
        }
      
    }
    [Serializable]
    public class Events_Bool : UnityEvent<bool> { };
}

 使用方式如下
未完待續 unity action

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章