Reputation: 1370
I create a custom editor that automatically creates gameobject, their components and automatically adding the listeners to the events.
but adding two persistent listener to the events does not work, but if i separate it, it will work.
[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;
}
I believe the first element of the unityevent is being overwrite, because if i comment adding persistent listener for animator , the audio play will work.
Upvotes: 0
Views: 612
Reputation: 14012
// 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");
I'd hazard a guess that this is the issue:
script.Events = new UnityEvent();
You are doing this twice - once at the start, then once you've added an event listener to the Events
object, you are calling the above code again.
The above code will replace the original instance with a brand new UnityEvent()
object instance that doesn't have any attached event handlers. You then attach an event handler to it - hence the reason only one works.
Try this instead:
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");
Dislcaimer: I've never used Unity before - there could be another way to add multiple event handlers to a script.
Upvotes: 3