The event interaction of the UI must exist in the EventSystem interaction event system
Canvas Ray Emitter
: Whether to ignore the reverse ray operation
2d or 3d or all objects will block this event
The rendering layer of the object that occludes the event
Whether to receive Canvas rays
If you want a range without extra borders, you can rotate the button if you change the picture to transparent
Button component
Is it possible to interact
Interactive effect:
None no interaction effect
Color Tint color change
Switching of Sprite Swap images
Whether to enable keyboard navigation
call back
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class 回调 : MonoBehaviour
{
//声明一个委托
public UnityAction Callback;
void Start()
{
//指定委托
Callback = Success;
for(int i = 0;i < 10; i++)
{
Debug.Log(1);
}
//调用委托
Callback();
}
//Success方法
public void Success()
{
Debug.Log("Success");
}
}
button trigger event
1. Write the function to be triggered and place it on any game object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonAttack : MonoBehaviour
{
public void Buttonattack()
{
Debug.Log("1234");
}
}
2. Click to add press event
3. Drag the game object in and specify the method
Automatically add press event
/// <summary>
/// 自动添加按钮事件
/// </summary>
void Start()
{
//先获取要添加的位置
GameObject sub = GameObject.Find("/Canvas/Sumbit");
//获取button组件
Button button = sub.GetComponent<Button>();
//添加指定的方法
button.onClick.AddListener(Test);
}
void Test()
{
Debug.Log("789");
}
Toggle component
Group
Implement radio
1. Create an empty object first, then add Toggle Group
2. Add selected boxes to a group
Is it allowed to turn off every option
Slider component
Control volume with Slider
1. First add the game object to the sound component (Audio Source)
2. Write a script to control the sound, and put it on a game object with the sound component
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangerSlider : MonoBehaviour
{
public void changeSlider(float v)
{
//获得slider传入的值,并且把这个值给音量
this.gameObject.GetComponent<AudioSource>().volume = v;
}
}
3. Specify the Slider component
Input File component
InputField event
When the input content of the text box changes, execute the callback function
When the text box finishes typing, execute the callback function
public class InputText : MonoBehaviour
{
public void OnChage(string name)
{
Debug.Log("Change:" + name);
}
public void OnEnd(string name)
{
Debug.Log("End:" + name);
}
}