简体   繁体   中英

How to access the OnClick event via Script

I am trying to do a SongManager object which will be responsible for changing songs. I created a SongManager and a Song script. On runtime SongManager creates as many song buttons as i want each with different variables. Everything seems to work fine except that i cant get to OnClick event to change the songs.I tried many things but i guess its all outdated. Stuff like:

public Button.ButtonClickedEvent OnClickEvent;

or

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

Appreciate any help, thank u guys.

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

You can use Events for comminucation with your other classes. Here is the code :

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;
    }
}

First of all, i create a Initialize method. It uses Song object variables for initialization and takes an index as button id. After i create an event for notify listeners with index.

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);
        } 
    }
}

When SongManager became enable, it starts to listen to OnSongButtonClick events. This way, you can know which button clicked.

I hope it helps.

You pass in the function name to the AddListener parameter.

public GameObject go;
Button myButton = null;

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

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

You can also do: myButton.onClick.AddListener(delegate { myCallBackFunction(); });

Note:

Please post your code next time instead of posting a screenshot of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM