同步之前的修改

This commit is contained in:
2025-05-24 16:01:59 +08:00
parent a510a86ce5
commit e5edc8f863
8 changed files with 61 additions and 143 deletions

View File

@@ -3,14 +3,12 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using XericLibrary.Runtime.CustomEditor;
using Object = UnityEngine.Object;
using Sirenix.OdinInspector;
#if UNITY_EDITOR
using Sirenix.Utilities.Editor;
@@ -25,27 +23,28 @@ namespace XericLibrary.Runtime.MacroLibrary
{
#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)
{
if (toggleGroup == null)
{
throw new ArgumentNullException(nameof(toggleGroup));
}
if (togglesFieldInfo == null)
{
throw new InvalidOperationException("Unable to access the 'm_Toggles' field.");
}
var toggles = togglesFieldInfo.GetValue(toggleGroup) as List<Toggle>;
if (toggles == null)
{
throw new InvalidCastException("The 'm_Toggles' field is not of type List<Toggle>.");
}
return toggles;
if (toggleGroup == null)
{
throw new ArgumentNullException(nameof(toggleGroup));
}
if (togglesFieldInfo == null)
{
throw new InvalidOperationException("Unable to access the 'm_Toggles' field.");
}
var toggles = togglesFieldInfo.GetValue(toggleGroup) as List<Toggle>;
if (toggles == null)
{
throw new InvalidCastException("The 'm_Toggles' field is not of type List<Toggle>.");
}
return toggles;
// return (List<Toggle>)togglesFieldInfo.GetValue(toggleGroup);
}
@@ -78,7 +77,6 @@ namespace XericLibrary.Runtime.MacroLibrary
return toggleGroup.GetToggles().Count;
}
/// <summary>
/// 在单选项组上注册一个事件,当组中的任意成员变成激活状态时调用(其他的不会发生调用)。
@@ -96,6 +94,7 @@ namespace XericLibrary.Runtime.MacroLibrary
});
}
}
/// <summary>
/// 清空单选项组中的所有事件(与注册所有事件对应,但那个事件没法单独注销)
/// </summary>
@@ -107,7 +106,7 @@ namespace XericLibrary.Runtime.MacroLibrary
toggle.onValueChanged.RemoveAllListeners();
}
}
#region toggle
/// <summary>
@@ -136,29 +135,28 @@ namespace XericLibrary.Runtime.MacroLibrary
#region
[LabelText("单选组")] public ToggleGroup ToggleGroup;
[SerializeField, LabelText("编辑单选项目顺序")]
[ListDrawerSettings(OnTitleBarGUI = "GetAndSortToggle")]
[SerializeField, LabelText("编辑单选项目顺序")] [ListDrawerSettings(OnTitleBarGUI = "GetAndSortToggle")]
private List<Toggle> toggleList = new List<Toggle>();
// 当前选中的项目
private int nowSelectToggleIndex = 0;
private Toggle nowSelectToggle = null;
public Transform TogglesContext => ToggleGroup.transform;
/// <summary>
/// 当前选中的toggle索引
/// </summary>
public int NowSelectToggleIndex => nowSelectToggleIndex;
/// <summary>
/// 当前选中的toggle
/// </summary>
public Toggle NowSelectToggle => nowSelectToggle;
/// <summary>
/// 获取并给列表排序(顺序不一定与拼音有关)
/// </summary>
@@ -168,11 +166,7 @@ namespace XericLibrary.Runtime.MacroLibrary
// 自动获取并排序
if (SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh))
{
var newToggleList = MacroSort.FullCharacterOrderSort(ToggleGroup.GetToggles(), a => a.name).ToList();
if (newToggleList.Count <= 0 || newToggleList == null)
Debug.LogError("如果无法更新获取自动排序toggle可能是因为toggleGroup被隐藏了手动将其激活后再获取即可。");
else
toggleList = newToggleList;
GetSortToggle();
}
// 反转顺序
if (SirenixEditorGUI.ToolbarButton(EditorIcons.TriangleDown))
@@ -180,12 +174,17 @@ namespace XericLibrary.Runtime.MacroLibrary
toggleList.Reverse();
}
#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)
Debug.LogError("如果无法更新获取自动排序toggle可能是因为toggleGroup被隐藏了手动将其激活后再获取即可。");
else
toggleList = newToggleList;
#endif
}
}
#endregion
@@ -226,7 +225,7 @@ namespace XericLibrary.Runtime.MacroLibrary
// 未指定组时,说明压根没用这部分功能,用不着初始化。
if (ToggleGroup == null)
return;
// 防呆警告
if (toggleList.Count <= 0)
{
@@ -237,13 +236,14 @@ namespace XericLibrary.Runtime.MacroLibrary
Debug.LogWarning($"在初始化单选项组时,{ToggleGroup.name}并未预先指定索引顺序,将默认使用大纲顺序。");
}
}
// 事件初始化
for (int i = 0; i < toggleList.Count; i++)
{
var toggle = toggleList[i];
ToggleAddEvent(toggle);
if (toggle.isOn)
{
nowSelectToggleIndex = i;
@@ -263,12 +263,12 @@ namespace XericLibrary.Runtime.MacroLibrary
public int AddToggle(Toggle t)
{
ToggleAddEvent(t);
var resultIndex = toggleList.Count;
toggleList.Add(t);
return resultIndex;
}
/// <summary>
/// 移除一个toggle这不会影响其他toggle的索引但此处移除的位置会为空。
/// <code>
@@ -282,12 +282,12 @@ namespace XericLibrary.Runtime.MacroLibrary
var index = toggleList.IndexOf(t);
if (index < 0)
return false;
t.onValueChanged.RemoveAllListeners();
t.onValueChanged.RemoveAllListeners();
toggleList[index] = null;
return true;
}
/// <summary>
/// 清除toggle
/// </summary>
@@ -300,11 +300,11 @@ namespace XericLibrary.Runtime.MacroLibrary
if (allowDestroy)
Object.Destroy(t);
}
toggleList.Clear();
}
/// <summary>
/// toggle注册的事件只有当按下时才需要调用此事件。
/// </summary>
@@ -316,7 +316,7 @@ namespace XericLibrary.Runtime.MacroLibrary
if (a) ToggleRegister(t);
});
}
/// <summary>
/// toggle注册的事件只有当按下时才需要调用此事件。
/// </summary>
@@ -328,9 +328,8 @@ namespace XericLibrary.Runtime.MacroLibrary
OnAnyToggleSwitchOn?.Invoke(t);
OnAnyToggleIndexSwitchOn?.Invoke(nowSelectToggleIndex);
}
/// <summary>
/// 获取toggle代表的索引
/// </summary>
@@ -361,6 +360,7 @@ namespace XericLibrary.Runtime.MacroLibrary
{
target.isOn = true;
}
/// <summary>
/// 设置单选项激活
/// </summary>
@@ -372,6 +372,7 @@ namespace XericLibrary.Runtime.MacroLibrary
SetToggleOn(toggleList[index]);
}
}
/// <summary>
/// 设置单选项激活
/// </summary>
@@ -380,6 +381,7 @@ namespace XericLibrary.Runtime.MacroLibrary
{
target.SetIsOnWithoutNotify(true);
}
/// <summary>
/// 设置单选项激活
/// </summary>
@@ -391,7 +393,7 @@ namespace XericLibrary.Runtime.MacroLibrary
SetToggleOnWithoutNotify(toggleList[index]);
}
}
/// <summary>
/// 清除映射结构(不会清除toggle实例)
@@ -411,11 +413,11 @@ namespace XericLibrary.Runtime.MacroLibrary
{
Object.Destroy(toggleList[i]);
}
Clear();
}
#endregion
}
#endregion
@@ -485,8 +487,8 @@ namespace XericLibrary.Runtime.MacroLibrary
/// <param name="target"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static RectTransform RectTransform<T>(this T target)
where T : Component
public static RectTransform RectTransform<T>(this T target)
where T : Component
=> target.transform as RectTransform;
#endregion