本文介绍了如何在Unity Editor脚本中将持久侦听器添加到Button.onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个简单的事情:

I am trying to do a simple thing:

  1. 创建一个新的GameObject
  2. 向游戏对象添加Button组件.
  3. 向按钮的onClick事件添加持久的侦听器.
  1. Create a new GameObject
  2. Add a Button component to the gameObject.
  3. Add a persistent Listener to button's onClick event.

我尝试注册的方法在其他脚本中.这是我正在尝试的代码:

The method I am trying to register is in some other script. Here is piece of code that I am trying:

MyScript myScriptInstance = FindObjectOfType<MyScript>(); 
var go = new GameObject();
var btn = go.AddComponent<Button>();

var targetinfo = UnityEvent.GetValidMethodInfo(myScriptInstance,
"OnButtonClick", new Type[]{typeof(GameObject)});

var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),go, targetinfo, false);
UnityEventTools.AddPersistentListener(btn.onClick, action);

MyScript.cs看起来像这样:

MyScript.cs looks like this:

public class MyScript : MonoBehaviour
{
    public void OnButtonClick(GameObject sender)
    {
        // do some stuff here.
    }
}

运行此代码时,Buttons Onclick侦听器为空,如下所示:

When I run this code, Buttons Onclick listener is empty like this:

如果我更改行

var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),
go, targetinfo, false);

var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),
go, targetinfo, true);

我明白了:

我关注了这些说明,但不知道这里出了什么问题.

I followed these instructions but don't know what went wrong here.

任何帮助都将受到感激.

Any kind of help is truly appreciated.

推荐答案

在花了很多时间之后,我得出的结论是这是一个错误.这是一个Mono错误.

After spending so much time on this, I came to conclusion that this is a bug. This is a Mono bug.

以下是参考文献:

参考1 参考2 参考3 参考4

Unity不会在短期内解决此问题.他们通常不修复与Mono有关的内容,因为他们已经在努力升级到最新的Mono运行时.

Unity wont fix this anytime soon. They usually don't fix stuff related to Mono since they are already working to upgrade to the latest Mono run-time.

幸运的是,还有其他两种解决方法:

Luckily, there are two other workarounds:

1 .将AddObjectPersistentListenerUnityAction与泛型参数一起使用,然后将泛型传递给Delegate.CreateDelegate函数.

1.Use AddObjectPersistentListener and UnityAction with generic parameter then pass in the generic to the Delegate.CreateDelegate function.

MyScript myScriptInstance = FindObjectOfType<MyScript>();
var go = new GameObject();
var btn = go.AddComponent<Button>();

var targetinfo = UnityEvent.GetValidMethodInfo(myScriptInstance,
"OnButtonClick", new Type[] { typeof(GameObject) });

UnityAction<GameObject> action = Delegate.CreateDelegate(typeof(UnityAction<GameObject>), myScriptInstance, targetinfo, false) as UnityAction<GameObject>;

UnityEventTools.AddObjectPersistentListener<GameObject>(btn.onClick, action, go);

2 .完全不要使用Delegate.CreateDelegate.只需使用AddObjectPersistentListener.

2.Don't use Delegate.CreateDelegate at-all. Simply use AddObjectPersistentListener.

MyScript myScriptInstance = FindObjectOfType<MyScript>();
var go = new GameObject();
var btn = go.AddComponent<Button>();

UnityAction<GameObject> action = new UnityAction<GameObject>(myScriptInstance.OnButtonClick);
UnityEventTools.AddObjectPersistentListener<GameObject>(btn.onClick, action, go);

这两种方法都能给您带来帮助:

Both if these gives you this:

第二种解决方案不需要通过反射找到函数.您必须在运行时绑定功能.第一个使用反射.

The second solution does not require finding the function with reflection. You must bind the function before run-time. The first one uses reflection.

您可能需要使用第一种解决方案,因为它与您正在执行的操作非常相似,并且可以将函数名称提供为字符串变量.

You probably need to use the first solution as it is very similar to what you are doing and you can provide the function name as a string variable.

这篇关于如何在Unity Editor脚本中将持久侦听器添加到Button.onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:48