Unity将自己定义的事件显示在Inspecter面板上

1.效果

img

2.代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class MyHandButton : MonoBehaviour
{
    //创建一个OnHandButtonClickedEvent的对象。这个将显示在inspecter面板上
    public OnHandButtonClickedEvent onHandButtonClickedEvent;

    //调用事件举例
    //当该物体被删除时调用
    private void OnDestroy()
    {
        if (onHandButtonClickedEvent != null)
        {
            //触发事件
            onHandButtonClickedEvent?.Invoke();
        }
    }
}

/// <summary>
/// 创建一个派生至untyEvent的类
/// </summary>
[Serializable]//将类标记为可序列化类,这样的自定义类才能序列化显示在inspecter面板上
public class OnHandButtonClickedEvent : UnityEvent{ }

3.参考

(5条消息) Unity在Inspecter面板中显示事件_程序员正茂的博客-CSDN博客

(5条消息) unity如何将自己定义的事件显示在Inspector面板_苦逼的程序员!!!的博客-CSDN博客

最后修改:2023 年 11 月 12 日
如果觉得我的文章对你有用,请随意赞赏