添加基础枚举扩展

This commit is contained in:
2023-10-29 17:56:31 +08:00
parent e2ca273696
commit e293c5592d
6 changed files with 456 additions and 10 deletions
+145 -9
View File
@@ -1,5 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace XericLibrary.Runtime.MacroLibrary
@@ -8,11 +11,91 @@ namespace XericLibrary.Runtime.MacroLibrary
{
#region 对象操作
/// <summary>
/// 获取此枚举器在给定索引下的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="target"></param>
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public static T SelectIndex<T>(this IEnumerable<T> target, int index)
{
int nowIndex = 0;
foreach(var item in target)
if(nowIndex++ == index)
return (T)item;
throw new IndexOutOfRangeException("给定的索引超出了枚举器的范围");
}
/// <summary>
/// 获取此枚举器在给定索引下的值,并且允许在无法找到的情况下使用null返回
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="target"></param>
/// <param name="index"></param>
/// <returns></returns>
public static T SelectIndexOrDefault<T>(this IEnumerable<T> target, int index)
where T : class
{
int nowIndex = 0;
foreach(var item in target)
if(nowIndex++ == index)
return (T)item;
return null;
}
/// <summary>
/// 按顺序获取一个节点的所有父级节点
/// </summary>
/// <param name="trans"></param>
/// <returns></returns>
public static IEnumerable<Transform> GetParents(this Transform trans)
{
Transform target = trans.parent;
while(target != null)
{
yield return target;
target = target.parent;
}
}
/// <summary>
/// 遍历项目中父级内所有子项的差距,并找到第1个差距最大的项目;
/// <code>
/// 具体是先从当前节点向上级找到第一个最少子项数量的节点,
/// 以此为基准,从当前最少数量节点的子集中返回来自项目的父级(或来自项目);
/// 具体是用来在场景中寻找第一个具有标识意义的节点
/// </code>
/// </summary>
/// <param name="trans"></param>
/// <param name="backStep">倒数步骤数,基于基准父级/param>
/// <returns></returns>
[Obsolete("未验证的,谨慎使用")]
public static Transform GetObjectNameInParentsFilterByChildrenMeanDifferen(this Transform trans, int backStep = 1)
{
int minParentIndex = 0;
int index = 0;
int minChildCount = int.MaxValue;
foreach(Transform parent in trans.GetParents())
{
if(parent.childCount < minChildCount)
{
minChildCount = parent.childCount;
minParentIndex = index;
}
index++;
}
return trans.GetParents().SelectIndexOrDefault(minParentIndex - backStep);
}
/// <summary>
/// 获取一个变换节点下所有的子成员
/// </summary>
/// <param name="trans"></param>
/// <param name="recursion">递归深度,0表示仅在第一子集中寻找,1表示在包含下一级在内的所有成员中寻找</param>
/// <returns></returns>
public static IEnumerable<Transform> GetChildren(this Transform trans)
{
@@ -23,34 +106,36 @@ namespace XericLibrary.Runtime.MacroLibrary
}
/// <summary>
/// 在所有子成员中深度优先找到第一个组件
/// 在所有子成员中深度优先找到第一个组件;
/// 注意这只会在子项中查找。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="trans"></param>
/// <param name="recursion">遍历深度,0表示仅在此处寻找,1表示在包含下一级在内的所有成员中寻找</param>
/// <param name="recursion">遍历深度,0表示仅在子集中寻找,1表示在子集中寻找,如果找不到的将在子集的子集中寻找,以此类推</param>
/// <returns></returns>
public static IEnumerable<T> GetChildrenRecursionComponents<T>(this Transform trans, int recursion = 1)
public static IEnumerable<T> GetComponentsInRecursionChildren<T>(this Transform trans, int recursion = 1)
where T : Component
{
int newRecursion = recursion - 1;
foreach(var item in trans.GetChildren())
{
var comp = item.GetComponent<T>();
if(comp == null && newRecursion > 0)
comp = item.GetChildrenRecursionComponent<T>(newRecursion);
comp = item.GetComponentInRecursionChildren<T>(newRecursion);
yield return comp;
}
}
/// <summary>
/// 按深度优先寻找一个组件
/// 按深度优先寻找一个组件;
/// 注意这只会在子项中查找。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="trans"></param>
/// <param name="depth">遍历深度,0表示仅在第一子集中寻找,1表示在包含下一级在内的所有成员中寻找</param>
/// <param name="depth">遍历深度,0表示仅在子集中寻找,1表示在子集中寻找,如果找不到的将在子集的子集中寻找,以此类推</param>
/// <returns></returns>
public static T GetChildrenRecursionComponent<T>(this Transform trans, int depth = 1)
public static T GetComponentInRecursionChildren<T>(this Transform trans, int depth = 1)
where T : Component
{
T result;
@@ -61,7 +146,7 @@ namespace XericLibrary.Runtime.MacroLibrary
{
result = item.GetComponent<T>();
if(result == null && newDepth > 0)
result = item.GetChildrenRecursionComponent<T>(newDepth);
result = item.GetComponentInRecursionChildren<T>(newDepth);
if(result != null)
return result;
}
@@ -125,5 +210,56 @@ namespace XericLibrary.Runtime.MacroLibrary
#endregion
#region 2023/10 过时
/// <summary>
/// 在所有子成员中深度优先找到第一个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="trans"></param>
/// <param name="recursion">遍历深度,0表示仅在此处寻找,1表示在包含下一级在内的所有成员中寻找</param>
/// <returns></returns>
[Obsolete("命名不符合规则,将使用GetComponentsInRecursionChildren替换")]
public static IEnumerable<T> GetChildrenRecursionComponents<T>(this Transform trans, int recursion = 1)
where T : Component
{
int newRecursion = recursion - 1;
foreach(var item in trans.GetChildren())
{
var comp = item.GetComponent<T>();
if(comp == null && newRecursion > 0)
comp = item.GetChildrenRecursionComponent<T>(newRecursion);
yield return comp;
}
}
/// <summary>
/// 按深度优先寻找一个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="trans"></param>
/// <param name="depth">遍历深度,0表示仅在第一子集中寻找,1表示在包含下一级在内的所有成员中寻找</param>
/// <returns></returns>
[Obsolete("命名不符合规则,将使用GetComponentsInRecursionChildren替换")]
public static T GetChildrenRecursionComponent<T>(this Transform trans, int depth = 1)
where T : Component
{
T result;
int newDepth = depth - 1;
foreach(var item in trans.GetChildren())
{
result = item.GetComponent<T>();
if(result == null && newDepth > 0)
result = item.GetChildrenRecursionComponent<T>(newDepth);
if(result != null)
return result;
}
return null;
}
#endregion
}
}