同步更新

This commit is contained in:
2025-06-09 09:10:06 +08:00
parent 0ead362241
commit f0235e6d18
6 changed files with 72 additions and 62 deletions
+2 -2
View File
@@ -2,8 +2,8 @@
"name": "Lrss3.SesothoLine.Editor", "name": "Lrss3.SesothoLine.Editor",
"rootNamespace": "SesothoLineEditor", "rootNamespace": "SesothoLineEditor",
"references": [ "references": [
"Lrss3.Deconstruction", "Lrss3.SesothoLine",
"UnityEditor.UI" "Lrss3.Deconstruction"
], ],
"includePlatforms": [ "includePlatforms": [
"Editor" "Editor"
Binary file not shown.
Binary file not shown.
Binary file not shown.
+70 -60
View File
@@ -3,19 +3,22 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.UI; using UnityEngine.UI;
using XericLibrary.Runtime.CustomEditor; using XericLibrary.Runtime.CustomEditor;
using Object = UnityEngine.Object;
#if ODIN_INSPECTOR
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
#if UNITY_EDITOR #endif
#if UNITY_EDITOR && ODIN_INSPECTOR
using Sirenix.Utilities.Editor; using Sirenix.Utilities.Editor;
#endif #endif
using Object = UnityEngine.Object;
namespace XericLibrary.Runtime.MacroLibrary namespace XericLibrary.Runtime.MacroLibrary
{ {
/// <summary> /// <summary>
@@ -25,27 +28,28 @@ namespace XericLibrary.Runtime.MacroLibrary
{ {
#region toggle 扩展 #region toggle 扩展
private static FieldInfo togglesFieldInfo = typeof(ToggleGroup).GetField("m_Toggles", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); private static FieldInfo togglesFieldInfo = typeof(ToggleGroup).GetField("m_Toggles",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
public static List<Toggle> GetToggles(this ToggleGroup toggleGroup) public static List<Toggle> GetToggles(this ToggleGroup toggleGroup)
{ {
if (toggleGroup == null) if (toggleGroup == null)
{ {
throw new ArgumentNullException(nameof(toggleGroup)); throw new ArgumentNullException(nameof(toggleGroup));
} }
if (togglesFieldInfo == null) if (togglesFieldInfo == null)
{ {
throw new InvalidOperationException("Unable to access the 'm_Toggles' field."); throw new InvalidOperationException("Unable to access the 'm_Toggles' field.");
} }
var toggles = togglesFieldInfo.GetValue(toggleGroup) as List<Toggle>; var toggles = togglesFieldInfo.GetValue(toggleGroup) as List<Toggle>;
if (toggles == null) if (toggles == null)
{ {
throw new InvalidCastException("The 'm_Toggles' field is not of type List<Toggle>."); throw new InvalidCastException("The 'm_Toggles' field is not of type List<Toggle>.");
} }
return toggles; return toggles;
// return (List<Toggle>)togglesFieldInfo.GetValue(toggleGroup); // return (List<Toggle>)togglesFieldInfo.GetValue(toggleGroup);
} }
@@ -78,7 +82,6 @@ namespace XericLibrary.Runtime.MacroLibrary
return toggleGroup.GetToggles().Count; return toggleGroup.GetToggles().Count;
} }
/// <summary> /// <summary>
/// 在单选项组上注册一个事件,当组中的任意成员变成激活状态时调用(其他的不会发生调用)。 /// 在单选项组上注册一个事件,当组中的任意成员变成激活状态时调用(其他的不会发生调用)。
@@ -96,6 +99,7 @@ namespace XericLibrary.Runtime.MacroLibrary
}); });
} }
} }
/// <summary> /// <summary>
/// 清空单选项组中的所有事件(与注册所有事件对应,但那个事件没法单独注销) /// 清空单选项组中的所有事件(与注册所有事件对应,但那个事件没法单独注销)
/// </summary> /// </summary>
@@ -107,7 +111,7 @@ namespace XericLibrary.Runtime.MacroLibrary
toggle.onValueChanged.RemoveAllListeners(); toggle.onValueChanged.RemoveAllListeners();
} }
} }
#region toggle 索引 #region toggle 索引
/// <summary> /// <summary>
@@ -134,45 +138,43 @@ namespace XericLibrary.Runtime.MacroLibrary
#endregion #endregion
#region 字段属性 #region 字段属性
#if ODIN_INSPECTOR
[LabelText("单选组")] public ToggleGroup ToggleGroup; [LabelText("单选组")]
#endif
[SerializeField, LabelText("编辑单选项目顺序")] public ToggleGroup ToggleGroup;
[ListDrawerSettings(OnTitleBarGUI = "GetAndSortToggle")] #if ODIN_INSPECTOR
[SerializeField, LabelText("编辑单选项目顺序")] [ListDrawerSettings(OnTitleBarGUI = "GetAndSortToggle")]
#endif
private List<Toggle> toggleList = new List<Toggle>(); private List<Toggle> toggleList = new List<Toggle>();
// 当前选中的项目 // 当前选中的项目
private int nowSelectToggleIndex = 0; private int nowSelectToggleIndex = 0;
private Toggle nowSelectToggle = null; private Toggle nowSelectToggle = null;
public Transform TogglesContext => ToggleGroup.transform; public Transform TogglesContext => ToggleGroup.transform;
/// <summary> /// <summary>
/// 当前选中的toggle索引 /// 当前选中的toggle索引
/// </summary> /// </summary>
public int NowSelectToggleIndex => nowSelectToggleIndex; public int NowSelectToggleIndex => nowSelectToggleIndex;
/// <summary> /// <summary>
/// 当前选中的toggle /// 当前选中的toggle
/// </summary> /// </summary>
public Toggle NowSelectToggle => nowSelectToggle; public Toggle NowSelectToggle => nowSelectToggle;
/// <summary> /// <summary>
/// 获取并给列表排序(顺序不一定与拼音有关) /// 获取并给列表排序(顺序不一定与拼音有关)
/// </summary> /// </summary>
public void GetAndSortToggle() public void GetAndSortToggle()
{ {
#if UNITY_EDITOR #if UNITY_EDITOR && ODIN_INSPECTOR
// 自动获取并排序 // 自动获取并排序
if (SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh)) if (SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh))
{ {
var newToggleList = MacroSort.FullCharacterOrderSort(ToggleGroup.GetToggles(), a => a.name).ToList(); GetSortToggle();
if (newToggleList.Count <= 0 || newToggleList == null)
Debug.LogError("如果无法更新获取自动排序toggle,可能是因为toggleGroup被隐藏了,手动将其激活后再获取即可。");
else
toggleList = newToggleList;
} }
// 反转顺序 // 反转顺序
if (SirenixEditorGUI.ToolbarButton(EditorIcons.TriangleDown)) if (SirenixEditorGUI.ToolbarButton(EditorIcons.TriangleDown))
@@ -180,12 +182,17 @@ namespace XericLibrary.Runtime.MacroLibrary
toggleList.Reverse(); toggleList.Reverse();
} }
#else #else
var newToggleList = MacroSort.FullCharacterOrderSort(ToggleGroup.GetToggles(), a => a.name).ToList(); GetSortToggle();
#endif
void GetSortToggle()
{
var newToggleList = MacroSort.FullCharacterOrderSort(ToggleGroup.GetToggles(), a => a.name)
.ToList();
if (newToggleList.Count <= 0 || newToggleList == null) if (newToggleList.Count <= 0 || newToggleList == null)
Debug.LogError("如果无法更新获取自动排序toggle,可能是因为toggleGroup被隐藏了,手动将其激活后再获取即可。"); Debug.LogError("如果无法更新获取自动排序toggle,可能是因为toggleGroup被隐藏了,手动将其激活后再获取即可。");
else else
toggleList = newToggleList; toggleList = newToggleList;
#endif }
} }
#endregion #endregion
@@ -226,7 +233,7 @@ namespace XericLibrary.Runtime.MacroLibrary
// 未指定组时,说明压根没用这部分功能,用不着初始化。 // 未指定组时,说明压根没用这部分功能,用不着初始化。
if (ToggleGroup == null) if (ToggleGroup == null)
return; return;
// 防呆警告 // 防呆警告
if (toggleList.Count <= 0) if (toggleList.Count <= 0)
{ {
@@ -237,13 +244,14 @@ namespace XericLibrary.Runtime.MacroLibrary
Debug.LogWarning($"在初始化单选项组时,{ToggleGroup.name}并未预先指定索引顺序,将默认使用大纲顺序。"); Debug.LogWarning($"在初始化单选项组时,{ToggleGroup.name}并未预先指定索引顺序,将默认使用大纲顺序。");
} }
} }
// 事件初始化 // 事件初始化
for (int i = 0; i < toggleList.Count; i++) for (int i = 0; i < toggleList.Count; i++)
{ {
var toggle = toggleList[i]; var toggle = toggleList[i];
ToggleAddEvent(toggle); ToggleAddEvent(toggle);
if (toggle.isOn) if (toggle.isOn)
{ {
nowSelectToggleIndex = i; nowSelectToggleIndex = i;
@@ -263,12 +271,12 @@ namespace XericLibrary.Runtime.MacroLibrary
public int AddToggle(Toggle t) public int AddToggle(Toggle t)
{ {
ToggleAddEvent(t); ToggleAddEvent(t);
var resultIndex = toggleList.Count; var resultIndex = toggleList.Count;
toggleList.Add(t); toggleList.Add(t);
return resultIndex; return resultIndex;
} }
/// <summary> /// <summary>
/// 移除一个toggle,这不会影响其他toggle的索引,但此处移除的位置会为空。 /// 移除一个toggle,这不会影响其他toggle的索引,但此处移除的位置会为空。
/// <code> /// <code>
@@ -282,12 +290,12 @@ namespace XericLibrary.Runtime.MacroLibrary
var index = toggleList.IndexOf(t); var index = toggleList.IndexOf(t);
if (index < 0) if (index < 0)
return false; return false;
t.onValueChanged.RemoveAllListeners(); t.onValueChanged.RemoveAllListeners();
toggleList[index] = null; toggleList[index] = null;
return true; return true;
} }
/// <summary> /// <summary>
/// 清除toggle /// 清除toggle
/// </summary> /// </summary>
@@ -300,11 +308,11 @@ namespace XericLibrary.Runtime.MacroLibrary
if (allowDestroy) if (allowDestroy)
Object.Destroy(t); Object.Destroy(t);
} }
toggleList.Clear(); toggleList.Clear();
} }
/// <summary> /// <summary>
/// toggle注册的事件,只有当按下时才需要调用此事件。 /// toggle注册的事件,只有当按下时才需要调用此事件。
/// </summary> /// </summary>
@@ -316,7 +324,7 @@ namespace XericLibrary.Runtime.MacroLibrary
if (a) ToggleRegister(t); if (a) ToggleRegister(t);
}); });
} }
/// <summary> /// <summary>
/// toggle注册的事件,只有当按下时才需要调用此事件。 /// toggle注册的事件,只有当按下时才需要调用此事件。
/// </summary> /// </summary>
@@ -328,9 +336,8 @@ namespace XericLibrary.Runtime.MacroLibrary
OnAnyToggleSwitchOn?.Invoke(t); OnAnyToggleSwitchOn?.Invoke(t);
OnAnyToggleIndexSwitchOn?.Invoke(nowSelectToggleIndex); OnAnyToggleIndexSwitchOn?.Invoke(nowSelectToggleIndex);
} }
/// <summary> /// <summary>
/// 获取toggle代表的索引 /// 获取toggle代表的索引
/// </summary> /// </summary>
@@ -361,6 +368,7 @@ namespace XericLibrary.Runtime.MacroLibrary
{ {
target.isOn = true; target.isOn = true;
} }
/// <summary> /// <summary>
/// 设置单选项激活 /// 设置单选项激活
/// </summary> /// </summary>
@@ -372,6 +380,7 @@ namespace XericLibrary.Runtime.MacroLibrary
SetToggleOn(toggleList[index]); SetToggleOn(toggleList[index]);
} }
} }
/// <summary> /// <summary>
/// 设置单选项激活 /// 设置单选项激活
/// </summary> /// </summary>
@@ -380,6 +389,7 @@ namespace XericLibrary.Runtime.MacroLibrary
{ {
target.SetIsOnWithoutNotify(true); target.SetIsOnWithoutNotify(true);
} }
/// <summary> /// <summary>
/// 设置单选项激活 /// 设置单选项激活
/// </summary> /// </summary>
@@ -391,7 +401,7 @@ namespace XericLibrary.Runtime.MacroLibrary
SetToggleOnWithoutNotify(toggleList[index]); SetToggleOnWithoutNotify(toggleList[index]);
} }
} }
/// <summary> /// <summary>
/// 清除映射结构(不会清除toggle实例) /// 清除映射结构(不会清除toggle实例)
@@ -411,11 +421,11 @@ namespace XericLibrary.Runtime.MacroLibrary
{ {
Object.Destroy(toggleList[i]); Object.Destroy(toggleList[i]);
} }
Clear(); Clear();
} }
#endregion #endregion
} }
#endregion #endregion
@@ -485,8 +495,8 @@ namespace XericLibrary.Runtime.MacroLibrary
/// <param name="target"></param> /// <param name="target"></param>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <returns></returns> /// <returns></returns>
public static RectTransform RectTransform<T>(this T target) public static RectTransform RectTransform<T>(this T target)
where T : Component where T : Component
=> target.transform as RectTransform; => target.transform as RectTransform;
#endregion #endregion
Binary file not shown.