2017-08-09 51 views
0

我創建了一個自動創建遊戲對象及其組件並自動將偵聽器添加到事件的自定義編輯器。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的第一個元素是被改寫,因爲如果我的評論增加了對動畫持續監聽器,音頻播放會工作。

enter image description here

+1

現在,我認爲你知道添加語言標記非常重要。 – Programmer

+1

您正在'Script'上設置'Events'屬性兩次。設置一次,然後將所有聽衆添加到「Events」對象 – Charleh

回答

3
// 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"); 

我大膽猜測,這是問題:

script.Events = new UnityEvent(); 

你這樣做兩次 - 一次在一開始,那麼一旦你添加一個事件收聽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"); 

Dislcaimer:我從來沒有使用統一前 - 有可能是另一種方式來多個事件處理程序添加到腳本中。

+0

其工作.... –