Archived
backup
This commit is contained in:
@@ -14,7 +14,8 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
#region 洗牌算法
|
||||
|
||||
/// <summary>
|
||||
/// Fisher-Yates洗牌算法,时间复杂度为O(n*n),空间复杂度为O(n)
|
||||
/// Fisher-Yates洗牌算法,时间复杂度为O(n*n),空间复杂度为O(n);
|
||||
/// 缺点是元素0很大概率会在最后出现
|
||||
/// </summary>
|
||||
/// <param name="length">待选项目的索引长度</param>
|
||||
/// <param name="select">输出项目的索引长度</param>
|
||||
@@ -39,6 +40,44 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 重复的洗牌
|
||||
/// </summary>
|
||||
/// <param name="length"></param>
|
||||
/// <param name="select"></param>
|
||||
/// <param name="repet"></param>
|
||||
/// <returns></returns>
|
||||
public static List<int> FisherYatesShuffle(int length, int select, int repet)
|
||||
{
|
||||
if(select > length)
|
||||
throw new ArgumentOutOfRangeException("在进行洗牌时,要求输出的项目比拥有的项目更多,这是不合理的。");
|
||||
#if UNITY_EDITOR
|
||||
if(repet > 100)
|
||||
Debug.LogError("洗牌的重复次数太高(> 100),已限制数量");
|
||||
#endif
|
||||
repet = Math.Clamp(repet, 1, 100);
|
||||
|
||||
var random = new System.Random();
|
||||
|
||||
List<int> arr = Enumerable.Range(0, length).ToList();
|
||||
List<int> res = new List<int>();
|
||||
|
||||
for(int j = 0; j < repet; j++)
|
||||
{
|
||||
for(int i = 0; i < length; i++)
|
||||
{
|
||||
int k = random.Next(arr.Count);
|
||||
res.Add(arr[k]);
|
||||
arr.RemoveAt(k);
|
||||
}
|
||||
arr = res.ToList();
|
||||
res.Clear();
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 蓄水池抽样算法,可以在线程安全的情况下完成洗牌。
|
||||
/// </summary>
|
||||
|
||||
@@ -99,19 +99,42 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Transform> GetChildren(this Transform trans)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if(trans == null)
|
||||
throw new Exception("在准备获取所有子成员时,节点为空");
|
||||
#endif
|
||||
|
||||
for(int i = 0; i < trans.childCount; i++)
|
||||
{
|
||||
yield return trans.GetChild(i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 递归获取一个变换节点下所有的子成员
|
||||
/// </summary>
|
||||
/// <param name="trans"></param>
|
||||
/// <param name="recursion">递归深度,1表示仅在第一子集中寻找,2表示在包含下一级在内的所有成员中寻找</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Transform> GetChildrenRecursion(this Transform trans, int recursion = 1)
|
||||
{
|
||||
int newdepth = recursion - 1;
|
||||
foreach(var child in trans.GetChildren())
|
||||
{
|
||||
if(newdepth > 0)
|
||||
foreach(var item in child.GetChildrenRecursion(newdepth))
|
||||
yield return item;
|
||||
yield return child;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在所有子成员中深度优先找到第一个组件;
|
||||
/// 注意这只会在子项中查找。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="trans"></param>
|
||||
/// <param name="recursion">遍历深度,0表示仅在子集中寻找,1表示在子集中寻找,如果找不到的将在子集的子集中寻找,以此类推</param>
|
||||
/// <param name="recursion">遍历深度,1表示仅在子集中寻找,2表示在子集中寻找,如果找不到的将在子集的子集中寻找,以此类推</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> GetComponentsInRecursionChildren<T>(this Transform trans, int recursion = 1)
|
||||
where T : Component
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
/// <summary>
|
||||
/// 变换宏
|
||||
/// </summary>
|
||||
public static class MacroTransform
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 变换封装类
|
||||
/// </summary>
|
||||
public class Conversion
|
||||
{
|
||||
#region 字段属性
|
||||
|
||||
/// <summary>
|
||||
/// 目标变换组件
|
||||
/// </summary>
|
||||
private Transform transform;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 使用方法
|
||||
|
||||
public Conversion(Transform target)
|
||||
{
|
||||
transform = target;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0fab5c35f2f4c340aa0f55f743eec10
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user