源码4:Selectable
前面以及讲完了UGUI的输入模块和事件系统,下面要分析一下UGUI的相关控件。对于UI画布上的可视化元素,包括Image/RawImage/Text,存在一个共同的基类Graphic。对于事件系统,每一个可执行操作的元素,例如Button/Toggle等,也存在一个基类Selectable。 本文会谈谈Selectable的实现
Selectable
简单看下Selectable的定义
/// <summary>
/// Simple selectable object - derived from to create a selectable control.
/// </summary>
public class Selectable
:
UIBehaviour,
IMoveHandler,
IPointerDownHandler, IPointerUpHandler,
IPointerEnterHandler, IPointerExitHandler,
ISelectHandler, IDeselectHandler
{
protected static Selectable[] s_Selectables = new Selectable[10];
protected static int s_SelectableCount = 0;
private bool m_EnableCalled = false;
/// <summary>
/// Copy of the array of all the selectable objects currently active in the scene.
/// </summary>
/// <example>
/// <code>
/// using UnityEngine;
/// using System.Collections;
/// using UnityEngine.UI; // required when using UI elements in scripts
///
/// public class Example : MonoBehaviour
/// {
/// //Displays the names of all selectable elements in the scene
/// public void GetNames()
/// {
/// foreach (Selectable selectableUI in Selectable.allSelectablesArray)
/// {
/// Debug.Log(selectableUI.name);
/// }
/// }
/// }
/// </code>
/// </example>
....
}
可以看到Selectable 继承了很多的事件接口。包括移动 按下,抬起 鼠标进入 鼠标出来 选中和取消选中
在这里面还定义了许多通用的字段属性
Transition 过度相关
Selectable状态改变时 的具体执行的过度类型
/// <summary>
///Transition mode for a Selectable.
/// </summary>
public enum Transition
{
/// <summary>
/// No Transition.
/// </summary>
None,
/// <summary>
/// Use an color tint transition.
/// </summary>
ColorTint,
/// <summary>
/// Use a sprite swap transition.
/// </summary>
SpriteSwap,
/// <summary>
/// Use an animation transition.
/// </summary>
Animation
}
// Type of the transition that occurs when the button state changes.
[FormerlySerializedAs("transition")]
[SerializeField]
private Transition m_Transition = Transition.ColorTint;
Selectable 的状态枚举
protected enum SelectionState
{
/// <summary>
/// The UI object can be selected.
/// </summary>
Normal,
/// <summary>
/// The UI object is highlighted.
/// </summary>
Highlighted,
/// <summary>
/// The UI object is pressed.
/// </summary>
Pressed,
/// <summary>
/// The UI object is selected
/// </summary>
Selected,
/// <summary>
/// The UI object cannot be selected.
/// </summary>
Disabled,
}
Normal : 正常状态,未被选中 通常游戏刚启动的都是这个状态
Highlighted 高亮状态 通常鼠标进入这个Selectable范围中(Pointer Enter)的时候
Pressed 按下状态
Selected 选择状态 但按Selectable压过后 松开鼠标或手指而且没有在点击任何UI元素 这个时候就是选择状态
Disable 当被禁用的时候
四种过度类型(Transition ) 都包含上面5种状态 每种类型具体表现就由下面参数进行配置
//默认ColorTint类型
[FormerlySerializedAs("transition")]
[SerializeField]
private Transition m_Transition = Transition.ColorTint;
//ColorTint类型 时对应的参数配置(主要是配置五种状态颜色)
// Colors used for

本文深入剖析Unity UGUI的Selectable组件,它是UI控件如Button、Toggle等的基础,涉及事件接口如移动、点击、选中等。文章详细解释了Selectable的状态枚举、过渡类型及其配置,以及Navigation导航系统的工作原理,阐述了如何根据用户输入进行UI元素的交互和状态变化。

3904

被折叠的 条评论
为什么被折叠?



