using System.Collections.Generic; using UnityEngine; namespace Gameplay { public static class ArrayEx { public static List RandomArrayFromArray(List list, int count) { if (list.Count < count) { throw new System.Exception("传入list长度不足"); } var cloneList = new List(list); var result = new List(); for (var i = 0; i < count; ++i) { var index = Random.Range(0, cloneList.Count); result.Add(cloneList[index]); cloneList.RemoveAt(index); } return result; } public static T RandomFromArray(List list) { var randomIndex = Random.Range(0, list.Count); return list[randomIndex]; } public static T RandomFromArray(T[] list) { var randomIndex = Random.Range(0, list.Length); return list[randomIndex]; } public static T RandomWithWeights(List list, List weights) { var totalWeight = 0; for (var i = 0; i < weights.Count; ++i) { totalWeight += weights[i]; } var randomValue = Random.Range(0, totalWeight); var currentWeight = 0; for (var i = 0; i < weights.Count; ++i) { currentWeight += weights[i]; if (randomValue < currentWeight) { return list[i]; } } return list[^1]; } } }