2016-11-14 41 views
0

我想要做一個SongManager對象,它將負責更改歌曲。我創建了一個SongManager和一個歌曲腳本。在運行時,SongManager創建儘可能多的歌曲按鈕,因爲我需要不同的變量。一切似乎工作正常,除了我不能去OnClick事件來改變歌曲。我嘗試了很多東西,但我想它全是過時的。這樣的東西:如何通過腳本訪問OnClick事件

public Button.ButtonClickedEvent OnClickEvent; 

go.GetComponent<Button>().onClick.AddListener(); 

得到任何幫助,感謝u人。

enter image description here

enter image description here

enter image description here

enter image description here

回答

1

您可以使用Events與其他班級進行學習。這裏是代碼:

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class SongButton : MonoBehaviour { 

    public delegate void SongButtonEvent(int index); 
    public static event SongButtonEvent OnSongButtonClick; 
    void ClickSongButton(){ 
     if (OnSongButtonClick != null) 
      OnSongButtonClick (index); 
    } 

    public Button button; 
    public Text songName; 
    public int unlocked; 
    public AudioClip clip; 
    public int index; 

    void Start() { 
     button.onClick.AddListener (ClickSongButton); 
    } 

    public void Initialize(int index,Song song){ 
     this.index = index; 
     songName.text = song.songName; 
     unlocked = song.unlocked; 
     clip = song.clip; 
     button.interactable = song.isInteractable; 
    } 
} 

首先,我創建一個Initialize方法。它使用Song對象變量進行初始化,並將索引作爲按鈕ID。之後我創建一個事件通知偵聽器索引。

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
using System.Collections.Generic; 

[System.Serializable] 
public class Song 
{ 
    public string songName; 
    public int unlocked; 
    public bool isInteractable; 
    public AudioClip clip; 
} 

public class SongManager : MonoBehaviour 
{ 

    public SongButton button; 
    public Transform panel; 
    public List<Song> songList; 

    void OnEnable(){ 
     SongButton.OnSongButtonClick += SongButton_OnSongButtonClick; 
    } 

    void OnDisable(){ 
     SongButton.OnSongButtonClick -= SongButton_OnSongButtonClick; 
    } 

    void SongButton_OnSongButtonClick (int index) 
    { 
     Debug.Log ("index : " + index + " - song name : " + songList [index].songName); 
    } 

    void Start() 
    { 
     FillList();  
    } 

    void FillList() 
    { 
     for (int i = 0; i < songList.Count; i++) { 
      SongButton songButton = Instantiate (button) as SongButton; 
      songButton.Initialize (i, songList [i]); 
      songButton.transform.SetParent (panel, false); 
     } 
    } 
} 

當SongManager啓用時,它開始偵聽OnSongButtonClick事件。這樣,你可以知道點擊了哪個按鈕。

我希望它有幫助。

0

您傳遞函數名到AddListener參數。

public GameObject go; 
Button myButton = null; 

void Start() 
{ 
    myButton = go.GetComponent<Button>(); 
    myButton.onClick.AddListener(() => myCallBackFunction()); 
} 

void myCallBackFunction() 
{ 
    Debug.Log("Button Clicked!"); 
} 

你也可以這樣做:myButton.onClick.AddListener(delegate { myCallBackFunction(); });

注:

請發表您的代碼下一個時間,而不是發佈它的屏幕截圖。

+0

或只是myButton.onClick.AddListener(myCallBackFunction);它更簡單。 – luizcarlosfx

+0

@luizcarlosfx是的,它更簡單。我通常與這兩個一起去,因爲它們允許您使用**參數**傳遞自定義函數。讓我們說一個爲某些動態UI在for循環中生成的按鈕。你評論中的那個不能這樣做。 – Programmer

+0

是的,但在大多數情況下,我更喜歡這樣做。你也可以創建一個沒有參數的函數,只用它來做你想做的任何事情。我更可讀。 – luizcarlosfx