Unity UGUI system combing- Button

Button component

Used to initiate or confirm an action in response to a click from the user
insert image description here

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)
insert image description here

2、Transition Transitions when keys are interacted with by the user

insert image description here

2.1 None (do not use transition)

insert image description here

2.2 Color Tint (color tinting transition)

Color Tint mode transitions by defining the color of different behaviors
insert image description here

  • 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
    insert image description here

  • Color Multiplier: Color multiplier
    After actual operation, I understand its effect as making the color of the current state brighter
    insert image description hereinsert image description here

  • 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
insert image description here

2.4 Animation animation

insert image description here

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.
insert image description here

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

Guess you like

Origin blog.csdn.net/Brave_boy666/article/details/126916282