提供了xericimage,这个组件可以帮助我们追踪toggle的checkmark事件,因为toggle的事件都没有提供重写,目前的c#版本也不支持钩子,所以这样是比较优的方案。

This commit is contained in:
2026-04-22 18:10:43 +08:00
parent e91aa14e9e
commit 296beac104
5 changed files with 188 additions and 49 deletions
+59
View File
@@ -0,0 +1,59 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using XericUI.Helper;
namespace XericUI.OverrideUI
{
/// <summary>
/// 图像组件
/// </summary>
/// <remarks>
/// 这个组件主要为了提供graphic层的事件,用于应对部分无法重写的ui组件事件
/// </remarks>
public class XericImage : Image
{
/// <summary>
/// 针对toggle的回报(ison)
/// </summary>
internal Action<bool> OnToggleCrossFadeCall;
public override void CrossFadeColor(Color targetColor, float duration, bool ignoreTimeScale, bool useAlpha, bool useRGB)
{
base.CrossFadeColor(targetColor, duration, ignoreTimeScale, useAlpha, useRGB);
OnToggleCrossFadeCall?.Invoke(targetColor.a > 0);
}
#region 组件替换
#if UNITY_EDITOR
[UnityEditor.MenuItem("CONTEXT/Image/Replace To XericImage", true)]
private static bool ValidateReplaceToXeric(UnityEditor.MenuCommand command)
=> command.context is Image and not XericImage;
[UnityEditor.MenuItem("CONTEXT/Image/Replace To XericImage", false, 10)]
private static void ReplaceToXeric(UnityEditor.MenuCommand command)
=> command.ReplaceCommandContext<Image, XericImage, Sprite>(
o =>
o.sprite,
(t, d) =>
{
t.sprite = d;
});
[UnityEditor.MenuItem("CONTEXT/Image/Replace To Image", true)]
private static bool ValidateReplaceToUnity(UnityEditor.MenuCommand command)
=> command.context is XericImage;
[UnityEditor.MenuItem("CONTEXT/Image/Replace To Image", false, 10)]
private static void ReplaceToUnity(UnityEditor.MenuCommand command)
=> command.ReplaceCommandContext<XericImage, Image, Sprite>(
o =>
o.sprite,
(t, d) =>
{
t.sprite = d;
});
#endif
#endregion
}
}