C# Unity添加第二个PersistentListener不工作

C# Unity添加第二个PersistentListener不工作,c#,unity3d,C#,Unity3d,我创建了一个自定义编辑器,自动创建游戏对象及其组件,并自动将侦听器添加到事件中 但是,向事件添加两个持久侦听器不起作用,但如果我将其分离,它将起作用 [MenuItem("GameObject/Create Animation/With SFX", false, -1)] static void CreateAnimationWithSFX() { GameObject go = new GameObject("animationWithSFX"); go.transform.S

我创建了一个自定义编辑器,自动创建游戏对象及其组件,并自动将侦听器添加到事件中

但是,向事件添加两个持久侦听器不起作用,但如果我将其分离,它将起作用

[MenuItem("GameObject/Create Animation/With SFX", false, -1)]
static void CreateAnimationWithSFX()
{
    GameObject go = new GameObject("animationWithSFX");
    go.transform.SetParent(Selection.activeTransform);

    AudioSource audioSource = go.AddComponent<AudioSource>();
    audioSource.playOnAwake = false;
    AudioMixer mixer = Resources.Load("Master") as AudioMixer;
    audioSource.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];

    SceneAnimationEvent script = go.AddComponent<SceneAnimationEvent>();

    // audioSource.Play
    script.Events = new UnityEvent();
    UnityAction methodDelegate = System.Delegate.CreateDelegate (typeof(UnityAction), audioSource, "Play") as UnityAction;
    UnityEventTools.AddPersistentListener (script.Events, methodDelegate);

    // animator.Play(string) - its only add this animator
    script.Events = new UnityEvent();
    UnityAction<string> methodDelegateString = System.Delegate.CreateDelegate(typeof(UnityAction<string>), Selection.activeGameObject.GetComponent<Animator>(), "Play") as UnityAction<string>;
    UnityEventTools.AddStringPersistentListener(script.Events, methodDelegateString, "lala");

    Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
    Selection.activeObject = go;
}
[MenuItem(“游戏对象/创建动画/使用SFX”,false,-1)]
静态void CreateAnimationWithFX()
{
GameObject go=新游戏对象(“AnimationWithFX”);
go.transform.SetParent(Selection.activeTransform);
AudioSource AudioSource=go.AddComponent

您将执行两次此操作—一次在开始时,然后将事件侦听器添加到
Events
对象后,再次调用上述代码

上面的代码将用一个全新的
UnityEvent()
对象实例替换原始实例,该实例没有任何附加的事件处理程序。然后将事件处理程序附加到该实例上-因此,只有一个事件处理程序起作用

请尝试以下方法:

    script.Events = new UnityEvent(); // Only create the UnityEvent instance once
    UnityAction methodDelegate = System.Delegate.CreateDelegate (typeof(UnityAction), audioSource, "Play") as UnityAction;
    UnityEventTools.AddPersistentListener (script.Events, methodDelegate);

    UnityAction<string> methodDelegateString = System.Delegate.CreateDelegate(typeof(UnityAction<string>), Selection.activeGameObject.GetComponent<Animator>(), "Play") as UnityAction<string>;
    UnityEventTools.AddStringPersistentListener(script.Events, methodDelegateString, "lala");
script.Events=new UnityEvent();//只创建一次UnityEvent实例
UnityAction methodDelegate=System.Delegate.CreateDelegate(类型为(UnityAction),音频源,“播放”)为UnityAction;
UnityEventTools.AddPersistentListener(script.Events,methodDelegate);
UnityAction methodDelegateString=System.Delegate.CreateDelegate(typeof(UnityAction),Selection.activeGameObject.GetComponent(),“Play”)作为UnityAction;
UnityEventTools.AddStringPersistentListener(script.Events,methodDelegateString,“lala”);
Dislcaimer:我以前从未使用过Unity——可能有另一种方法可以将多个事件处理程序添加到脚本中

您将执行两次此操作—一次在开始时,然后将事件侦听器添加到
Events
对象后,再次调用上述代码

上面的代码将用一个全新的
UnityEvent()
对象实例替换原始实例,该实例没有任何附加的事件处理程序。然后将事件处理程序附加到该实例上-因此,只有一个事件处理程序起作用

请尝试以下方法:

    script.Events = new UnityEvent(); // Only create the UnityEvent instance once
    UnityAction methodDelegate = System.Delegate.CreateDelegate (typeof(UnityAction), audioSource, "Play") as UnityAction;
    UnityEventTools.AddPersistentListener (script.Events, methodDelegate);

    UnityAction<string> methodDelegateString = System.Delegate.CreateDelegate(typeof(UnityAction<string>), Selection.activeGameObject.GetComponent<Animator>(), "Play") as UnityAction<string>;
    UnityEventTools.AddStringPersistentListener(script.Events, methodDelegateString, "lala");
script.Events=new UnityEvent();//只创建一次UnityEvent实例
UnityAction methodDelegate=System.Delegate.CreateDelegate(类型为(UnityAction),音频源,“播放”)为UnityAction;
UnityEventTools.AddPersistentListener(script.Events,methodDelegate);
UnityAction methodDelegateString=System.Delegate.CreateDelegate(typeof(UnityAction),Selection.activeGameObject.GetComponent(),“Play”)作为UnityAction;
UnityEventTools.AddStringPersistentListener(script.Events,methodDelegateString,“lala”);

Dislcaimer:我以前从未使用过Unity-可能有另一种方法可以将多个事件处理程序添加到脚本中。

现在,我认为您知道添加语言标记非常重要。您在
脚本上设置了两次
事件
属性。设置一次,然后将所有侦听器添加到
事件
对象中现在,我想您应该知道添加语言标记非常重要。您在
Script
上设置了两次
Events
属性。设置一次,然后将所有侦听器添加到
Events
对象
    script.Events = new UnityEvent(); // Only create the UnityEvent instance once
    UnityAction methodDelegate = System.Delegate.CreateDelegate (typeof(UnityAction), audioSource, "Play") as UnityAction;
    UnityEventTools.AddPersistentListener (script.Events, methodDelegate);

    UnityAction<string> methodDelegateString = System.Delegate.CreateDelegate(typeof(UnityAction<string>), Selection.activeGameObject.GetComponent<Animator>(), "Play") as UnityAction<string>;
    UnityEventTools.AddStringPersistentListener(script.Events, methodDelegateString, "lala");