简体   繁体   English

如何通过脚本访问OnClick事件

[英]How to access the OnClick event via Script

I am trying to do a SongManager object which will be responsible for changing songs. 我试图做一个SongManager对象,它将负责更改歌曲。 I created a SongManager and a Song script. 我创建了一个SongManager和一个Song脚本。 On runtime SongManager creates as many song buttons as i want each with different variables. 在运行时,SongManager会根据需要创建尽可能多的歌曲按钮,每个按钮都具有不同的变量。 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. 除了无法通过OnClick事件更改歌曲以外,其他所有内容似乎都正常运行。我尝试了很多尝试,但我想一切都已经过时了。 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. 首先,我创建一个Initialize方法。 It uses Song object variables for initialization and takes an index as button id. 它使用Song对象变量进行初始化,并使用索引作为按钮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. SongManager启用后,它将开始侦听OnSongButtonClick事件。 This way, you can know which button clicked. 这样,您可以知道单击了哪个按钮。

I hope it helps. 希望对您有所帮助。

You pass in the function name to the AddListener parameter. 您将函数名称传递给AddListener参数。

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(); }); 您还可以执行以下操作: myButton.onClick.AddListener(delegate { myCallBackFunction(); });

Note: 注意:

Please post your code next time instead of posting a screenshot of it. 请下次发布您的代码,而不是发布其屏幕截图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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