public UI.Button.ButtonClickedEvent onClick ;

描述

按下按钮时触发的 UnityEvent。

注意:在同一对象上发生 MouseDown 之后,在发生 MouseUp 时触发。

//To use this example, attach this script to an empty GameObject.
//Create two buttons (Create>UI>Button). Next, click your empty GameObject in the Hierarchy and click and drag each of your Buttons from the Hierarchy to the Your Button and "Your Second Button" fields in the Inspector.
//Click the Button in Play Mode to output the message to the console.

using UnityEngine; using UnityEngine.UI;

public class Example : MonoBehaviour { //Make sure to attach these Buttons in the Inspector public Button m_YourButton, m_YourSecondButton;

void Start() { Button btn = m_YourButton.GetComponent<Button>(); Button btn2 = m_YourSecondButton.GetComponent<Button>(); //Calls the TaskOnClick method when you click the Button btn.onClick.AddListener(TaskOnClick);

m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); }); }

void TaskOnClick() { //Output this to console when the Button is clicked Debug.Log("You have clicked the button!"); }

void TaskWithParameters(string message) { //Output this to console when the Button is clicked Debug.Log(message); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961