| 1234567891011121314151617181920212223242526272829303132333435363738 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace WildAttack
- {
- /// <summary>
- /// 根据权重随机
- /// </summary>
- public class WeightRandomUtils : Singleton<WeightRandomUtils>
- {
- public int GetRandomId(List<int> weightList)
- {
- int rdm = Random.Range(0, GetTotalWeight(weightList) + 1);
- int limit = 0;
- for (int i = 0; i < weightList.Count; i++)
- {
- limit += weightList[i];
- if (rdm <= limit)
- {
- return i + 1;
- }
- }
- return -1;
- }
- private int GetTotalWeight(List<int> weightList)
- {
- int total = 0;
- foreach (var item in weightList)
- {
- total += item;
- }
- return total;
- }
- }
- }
|