This commit is contained in:
陈思海 2025-12-22 13:41:19 +08:00
parent f74c1b5ada
commit 0e5ea2c357

View File

@ -19,12 +19,15 @@ namespace UnityEngine.UI
#endif #endif
{ {
[Serializable] [Serializable]
public class ToggleEvent : UnityEvent<bool> { } public class ToggleEvent : UnityEvent<bool>
{
}
public Toggle.ToggleTransition toggleTransition = Toggle.ToggleTransition.Fade; public Toggle.ToggleTransition toggleTransition = Toggle.ToggleTransition.Fade;
public Graphic graphic; public Graphic graphic;
[SerializeField] private UXGroup m_Group; [SerializeField] private UXGroup m_Group;
public UXGroup group public UXGroup group
{ {
get { return m_Group; } get { return m_Group; }
@ -37,10 +40,12 @@ namespace UnityEngine.UI
public ToggleEvent onValueChanged = new ToggleEvent(); public ToggleEvent onValueChanged = new ToggleEvent();
[Tooltip("Is the toggle currently on or off?")] [Tooltip("Is the toggle currently on or off?")] [SerializeField]
[SerializeField] private bool m_IsOn; private bool m_IsOn;
protected UXToggle() { } protected UXToggle()
{
}
#if UNITY_EDITOR #if UNITY_EDITOR
protected override void OnValidate() protected override void OnValidate()
@ -60,8 +65,13 @@ namespace UnityEngine.UI
#endif #endif
} }
public virtual void LayoutComplete() { } public virtual void LayoutComplete()
public virtual void GraphicUpdateComplete() { } {
}
public virtual void GraphicUpdateComplete()
{
}
protected override void OnDestroy() protected override void OnDestroy()
{ {
@ -147,6 +157,25 @@ namespace UnityEngine.UI
Set(value, false); Set(value, false);
} }
// 在 UXToggle 类内(建议放在类底部较靠近 Set 和 PlayEffect 的位置)加入:
// 覆盖 DoStateTransition保证 isOn 时视觉上总是 Selected除非 Disabled
protected override void DoStateTransition(Selectable.SelectionState state, bool instant)
{
// 如果被禁用,照常显示 Disabled
if (state == Selectable.SelectionState.Disabled)
{
base.DoStateTransition(state, instant);
return;
}
// 当 isOn 为 true 时,总是以 Selected 的样式渲染(但不改变 EventSystem 的真实 selection
if (m_IsOn)
state = Selectable.SelectionState.Selected;
base.DoStateTransition(state, instant);
}
void Set(bool value, bool sendCallback = true) void Set(bool value, bool sendCallback = true)
{ {
if (m_IsOn == value) if (m_IsOn == value)
@ -163,13 +192,23 @@ namespace UnityEngine.UI
} }
PlayEffect(toggleTransition == Toggle.ToggleTransition.None); PlayEffect(toggleTransition == Toggle.ToggleTransition.None);
if (sendCallback) if (sendCallback)
{ {
UISystemProfilerApi.AddMarker("Toggle.value", this); UISystemProfilerApi.AddMarker("Toggle.value", this);
onValueChanged.Invoke(m_IsOn); onValueChanged.Invoke(m_IsOn);
} }
// 触发选择态视觉刷新(当 isOn 为 true 显示 Selectedfalse 时回到 normal/hover/... 的计算结果)
// instant 参数:如果 ToggleTransition 是 None 我们使用 instant为保持和原来 PlayEffect 一致。
bool instant = (toggleTransition == Toggle.ToggleTransition.None);
// 传入的 state当 isOn 为 trueDoStateTransition 会将其替换为 Selected
// 当 isOn 为 false使用 currentSelectionState 以让正常的鼠标/键盘状态生效。
var stateToApply = m_IsOn ? Selectable.SelectionState.Selected : currentSelectionState;
DoStateTransition(stateToApply, instant);
} }
private void PlayEffect(bool instant) private void PlayEffect(bool instant)
{ {
if (graphic == null) if (graphic == null)
@ -183,6 +222,7 @@ namespace UnityEngine.UI
graphic.CrossFadeAlpha(m_IsOn ? 1f : 0f, instant ? 0f : 0.1f, true); graphic.CrossFadeAlpha(m_IsOn ? 1f : 0f, instant ? 0f : 0.1f, true);
} }
protected override void Start() protected override void Start()
{ {
PlayEffect(true); PlayEffect(true);