是否可以在不在 Unity 中添加新脚本的情况下添加 onClick() 事件操作(在 GameObject 下拉列表中)?

Is it possible to add a onClick() Event action(in GameObject dropdown) without adding new script in Unity?

我有一个 MRTK 板,它带有可按下按钮来关闭板。

按下关闭按钮时,Slate 会被禁用但不会被销毁。这是因为在Events部分>Interactable script>中默认设置了GameObject.SetActive()如图:

我想在点击关闭按钮后销毁石板。

我知道我可以通过制作一个脚本,将 slate 预制件附加到脚本(或者将按钮的父项作为父游戏对象,直到我得到 slate 作为父游戏对象),调用函数,然后使用 GameObject.Destroy().

但我想了解如何填充 Interactable 脚本中的 GameObject 下拉列表:

我知道显示其他下拉菜单,如 Transform、Follow me toggle、Solverhandler 等,因为它们附加到 Slate。

但是 GameObject 选项如何可用?这似乎是一个基本的东西,但我想确定它是如何编码的。

如果可以在 Gameobject 下拉列表中添加我的新动作,如何添加?

我花了一些时间查看 Interactable 和其他脚本,但我是 C# 的初学者,无法看到执行此操作的代码在哪里。

任何意见都会对我有所帮助。

不,你不能,因为 Object.Destroy is static and in UnityEventInteractable.OnClick 使用)通过 Inspector 你只能 select 实例方法。

你至少需要像

这样的最小组件
public class ObjectDestroyer : MonoBehaviour
{
    public void DestroyObject()
    {
        Destroy(this.gameObject);
    }
}

或者如果您不想通过 Inspector 执行此操作但会自动执行

public class ObjectDestroyer : MonoBehaviour
{
    // store the itneractable reference
    [Tootltip("If not provided uses the Interactable on this GameObject")]
    [SerialzieField] private Interactable _interactable;

    // Optionally provide a different target, otherwise destroys the Interactable
    [Tooltip("If not provided destroys the GameObject of the Interactable")]
    [SerializeField] private GameObject _targetToDestroy;

    private void Awake()
    {
        // use get component as fallback if no Interactable was provided
        if(!_interactable) _interactable = GetComponent<Interactable>();

        // attach the callback on runtime (won't appear in the inspector)
        _interactable.OnClick.AddListener(DestroyObject);
    }

    private void DestroyObject()
    {
        // destroy the object of the interactable if no target was provided
        if(!_targetToDestroy) _targetToDestroy = _interactable.gameObject;

        Destroy(_targetToDestroy);
    }
}