125 lines
3.7 KiB
C#
125 lines
3.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
using Random = UnityEngine.Random;
|
||
|
||
namespace Ichni
|
||
{
|
||
public static class GeneralExtensions
|
||
{
|
||
public static void Invoke(this Dictionary<string, UnityAction> dictionary)
|
||
{
|
||
foreach (var action in dictionary.Values)
|
||
{
|
||
action.Invoke();
|
||
}
|
||
}
|
||
|
||
public static void Invoke<T>(this Dictionary<string, UnityAction<T>> dictionary, T arg)
|
||
{
|
||
foreach (var action in dictionary.Values)
|
||
{
|
||
action.Invoke(arg);
|
||
}
|
||
}
|
||
|
||
public static void Invoke<T1, T2>(this Dictionary<string, UnityAction<T1, T2>> dictionary, T1 arg1, T2 arg2)
|
||
{
|
||
foreach (var action in dictionary.Values)
|
||
{
|
||
action.Invoke(arg1, arg2);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 遍历列表中的每个元素并执行指定的操作,用于foreach不能遍历的情况,例如在foreach中删除元素
|
||
/// </summary>
|
||
public static void For<T>(this IList<T> list, UnityAction<T> action)
|
||
{
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
action.Invoke(list[i]);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将列表中的元素从此列表转移到另一个列表
|
||
/// </summary>
|
||
public static void Transfer<T>(this List<T> fromList, List<T> toList, T item)
|
||
{
|
||
if (fromList.Contains(item))
|
||
{
|
||
fromList.Remove(item);
|
||
toList.Add(item);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"Item {item} not found in this List.");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在列表中交换两个元素
|
||
/// </summary>
|
||
public static void Swap<T>(this IList<T> list, int i, int j)
|
||
{
|
||
(list[i], list[j]) = (list[j], list[i]);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 随机打乱列表中的元素
|
||
/// </summary>
|
||
public static void Shuffle<T>(this IList<T> list)
|
||
{
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
list.Swap(i, Random.Range(i, list.Count));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 随机获取列表中的元素
|
||
/// </summary>
|
||
public static List<T> GetRandomElements<T>(this List<T> list, int count)
|
||
{
|
||
List<T> randomElements = new List<T>();
|
||
|
||
if (count >= list.Count)
|
||
{
|
||
randomElements = new List<T>(list);
|
||
return randomElements;
|
||
}
|
||
|
||
List<int> randomIndices = new List<int>();
|
||
while (randomIndices.Count < count)
|
||
{
|
||
int randomIndex = Random.Range(0, list.Count);
|
||
if (!randomIndices.Contains(randomIndex))
|
||
{
|
||
randomIndices.Add(randomIndex);
|
||
randomElements.Add(list[randomIndex]);
|
||
}
|
||
}
|
||
|
||
return randomElements;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除所有满足 predicate 的键值对。
|
||
/// </summary>
|
||
public static void RemoveWhere<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TKey, TValue, bool> predicate)
|
||
{
|
||
dict.Where(pair => predicate(pair.Key, pair.Value)).Select(pair => pair.Key).ToList().ForEach(key => dict.Remove(key));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取所有子物体
|
||
/// </summary>
|
||
public static List<Transform> GetAllChildren(this Transform parent)
|
||
{
|
||
return parent.Cast<Transform>().ToList();
|
||
}
|
||
}
|
||
} |