Button component
Used to initiate or confirm an action in response to a click from the user
Attributes
1、 Interactable Whether to accept incident response
If this is not checked, the Button will not be clickable in the scene (when creating a new Canvas or UI component, Unity will automatically create an EventSystem, which is used to monitor your mouse to trigger events in the UI, if you This EventSystem can be deleted if the event interaction that does not require the button)
2、Transition Transitions when keys are interacted with by the user
2.1 None (do not use transition)
2.2 Color Tint (color tinting transition)
Color Tint mode transitions by defining the color of different behaviors
-
Target Graphic: The target graphic
is usually bound to the Image component on the Button, which can be understood as the shading of the Button (that is, the image component on the button) -
Normal Color: normal color
-
Highlighted Color: Highlight color (the color when the control is highlighted)
when the mouse crosses the Button's color -
Pressed Color:
The color when the color control is pressed -
Selected Color: select color
-
Disabled Color: disabled color
-
Color Multiplier: Color multiplier
After actual operation, I understand its effect as making the color of the current state brighter
-
Fade Duration:
The time it takes to fade from one state to another (in seconds)
2.3 Sprite Swap (transition through the background image transformation on the button)
It is usually bound to the Image component on the Button, which can be understood as the shading of the Button
2.4 Animation animation
click event
Write a method to be executed when the button is clicked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestUI : MonoBehaviour
{
public void MyButtonEvent()
{
Debug.Log("OK");
}
}
Drag the object bound to the script to the specified position of the button component.
Dynamically add events using code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestButton : MonoBehaviour
{
void Start ()
{
GetComponent<Button>().onClick.AddListener(ButtonClick);
}
void ButtonClick()
{
Debug.Log("OK");
}
}