unityEvent 含参数的使用

本文详细介绍了Unity内置事件UnityEvent的使用方法,包括如何添加监听器、触发事件及事件响应函数的编写。UnityEvent支持传递简单类型的参数,如Bool、int、string等,但不支持自定义类作为参数。通过示例代码展示了如何在游戏开发中利用UnityEvent进行事件管理和响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值