MayHeCome/Assets/Scripts/Utils/ArrayUtils.cs

48 lines
1.0 KiB
C#
Raw Normal View History

2024-12-18 09:55:34 +00:00
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
public static class ArrayUtils
{
// const List<>
public static List<Ease> UIEasing = new List<Ease>()
{
Ease.InBack,
Ease.InSine,
Ease.InQuad,
Ease.InCirc,
Ease.InExpo
};
public static T RandomPick<T>(this List<T> pickedArray)
{
int rnd = Random.Range(0, pickedArray.Count);
return pickedArray[rnd];
}
public static T WeightPick<T>(this List<T> pickedArray, List<int> weight = null)
{
if (weight == null || weight.Count == 0)
{
weight = (from t in pickedArray select 1).ToList();
}
var totalWeight = weight.Sum();
int rnd = Random.Range(0, totalWeight);
for (int i = 0; i < pickedArray.Count; i++)
{
rnd -= weight[i];
if (rnd < 0)
{
return pickedArray[i];
}
}
return pickedArray[^1];
}
}