説明

ボタンが押されたときにトリガされる UnityEvent

Note: Triggered on MouseUp after MouseDown on the same object.

// To use this example, attach this script to an empty GameObject.
// Create three 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 First Button, Your Second Button
// and Your Third Button fields in the Inspector.
// Click each Button in Play Mode to output their 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_YourFirstButton, m_YourSecondButton, m_YourThirdButton;

void Start() { Button btn1 = m_YourFirstButton.GetComponent<Button>(); Button btn2 = m_YourSecondButton.GetComponent<Button>(); Button btn3 = m_YourThirdButton.GetComponent<Button>();

//Calls the TaskOnClick/TaskWithParameters method when you click the Button btn1.onClick.AddListener(TaskOnClick); btn2.onClick.AddListener(delegate {TaskWithParameters("Hello"); });

// add a lambda function version btn3.onClick.AddListener( () => ButtonClicked(42) ); }

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

void ButtonClicked(int m_value) { Debug.Log("Passed value: " + m_value); } }