一:实现思路
计算不同id对应的权重总和并加入到权重区间字典中,例如id1的区间为0-10,id2的区间为10-30,id3的区间为30-60.....随机获取值的时候将随机值和权重区间字典中的每一个值依次做比较
二:完整代码
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class Test : MonoBehaviour
{
private Dictionary<string, int> weightDict = new Dictionary<string, int>();//权重字典
private Dictionary<string, int> weightSectionDict = new Dictionary<string, int>();//权重区间字典
private void Awake()
{
weightDict.Add("Id1", 10);
weightDict.Add("Id2", 20);
weightDict.Add("Id3", 30);
weightDict.Add("Id4", 50);
weightDict.Add("Id5", 40);
//计算权重区间
CalcWeightSection();
}
/// <summary>