【问题标题】:How to access the OnClick event via Script如何通过脚本访问 OnClick 事件
【发布时间】:2017-03-28 00:15:53
【问题描述】:

我正在尝试创建一个 SongManager 对象,该对象将负责更改歌曲。我创建了一个 SongManager 和一个 Song 脚本。在运行时 SongManager 创建尽可能多的歌曲按钮,每个按钮都有不同的变量。一切似乎都很好,除了我无法进入 OnClick 事件来更改歌曲。我尝试了很多东西,但我想它已经过时了。像这样的东西:

public Button.ButtonClickedEvent OnClickEvent;

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

感谢任何帮助,谢谢你们。

【问题讨论】:

    标签: user-interface events audio unity3d buttonclick


    【解决方案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 事件。这样,您就可以知道点击了哪个按钮。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您将函数名称传递给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(); });

      注意:

      请下次发布您的代码,而不是发布它的屏幕截图。

      【讨论】:

      • 或者只是 myButton.onClick.AddListener(myCallBackFunction);它更简单。
      • @luizcarlosfx 是的,它更简单。我通常选择这两个,因为它们允许您使用 parameter 传入自定义函数。假设在 for 循环中为某些动态 UI 生成了一个按钮。您评论中的那个不能这样做。
      • 是的,但在大多数情况下,我更喜欢这样做。你也可以创建一个没有参数的函数,然后用它来做任何你想做的事情。我更具可读性。
      猜你喜欢
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多