Class Button
A standard button that sends an event when clicked.
Inherited Members
Namespace: UnityEngine.UI
Syntax
[AddComponentMenu("UI/Button", 30)]
public class Button : Selectable, IMoveHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, IPointerClickHandler, ISubmitHandler, IEventSystemHandler
Constructors
Button()
Declaration
protected Button()
Properties
onClick
UnityEvent that is triggered when the button is pressed. Note: Triggered on MouseUp after MouseDown on the same object.
Declaration
public Button.ButtonClickedEvent onClick { get; set; }
Property Value
Type | Description |
---|---|
Button.ButtonClickedEvent |
Examples
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ClickExample : MonoBehaviour
{
public Button yourButton;
void Start()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Debug.Log("You have clicked the button!");
}
}
Methods
OnPointerClick(PointerEventData)
Call all registered IPointerClickHandlers. Register button presses using the IPointerClickHandler. You can also use it to tell what type of click happened (left, right etc.). Make sure your Scene has an EventSystem.
Declaration
public virtual void OnPointerClick(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData | Pointer Data associated with the event. Typically by the event system. |
Implements
Examples
//Attatch this script to a Button GameObject
using UnityEngine;
using UnityEngine.EventSystems;
public class Example : MonoBehaviour, IPointerClickHandler
{
//Detect if a click occurs
public void OnPointerClick(PointerEventData pointerEventData)
{
//Use this to tell when the user right-clicks on the Button
if (pointerEventData.button == PointerEventData.InputButton.Right)
{
//Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
Debug.Log(name + " Game Object Right Clicked!");
}
//Use this to tell when the user left-clicks on the Button
if (pointerEventData.button == PointerEventData.InputButton.Left)
{
Debug.Log(name + " Game Object Left Clicked!");
}
}
}
OnSubmit(BaseEventData)
Call all registered ISubmitHandler.
Declaration
public virtual void OnSubmit(BaseEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
BaseEventData | eventData | Associated data with the event. Typically by the event system. |
Implements
Remarks
This detects when a Button has been selected via a "submit" key you specify (default is the return key).
To change the submit key, either:
Go to Edit->Project Settings->Input.
Next, expand the Axes section and go to the Submit section if it exists.
If Submit doesn’t exist, add 1 number to the Size field. This creates a new section at the bottom. Expand the new section and change the Name field to “Submit”.
Change the Positive Button field to the key you want (e.g. space).
Or:
Go to your EventSystem in your Project
Go to the Inspector window and change the Submit Button field to one of the sections in the Input Manager (e.g. "Submit"), or create your own by naming it what you like, then following the next few steps.
Go to Edit->Project Settings->Input to get to the Input Manager.
Expand the Axes section in the Inspector window. Add 1 to the number in the Size field. This creates a new section at the bottom.
Expand the new section and name it the same as the name you inserted in the Submit Button field in the EventSystem. Set the Positive Button field to the key you want (e.g. space)