我創建了一個自動創建遊戲對象及其組件並自動將偵聽器添加到事件的自定義編輯器。Unity add second PersistentListener not working
但添加兩個持久偵聽器的事件不起作用,但如果我分開它,它將工作。
[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;
}
相信unityevent的第一個元素是被改寫,因爲如果我的評論增加了對動畫持續監聽器,音頻播放會工作。
現在,我認爲你知道添加語言標記非常重要。 – Programmer
您正在'Script'上設置'Events'屬性兩次。設置一次,然後將所有聽衆添加到「Events」對象 – Charleh