// 创建普通Unit private Unit CreateNormalUnit(Tile tile, int level = -1) { if (level == -1) // 未指定等级,就用随机等级 { level = RandomController.Range(GlobalValue.NormalLevelList, GlobalValue.NormalProbList); } Transform unitTr = Instantiate(unitPrefab); Unit unit = unitTr.GetComponent <Unit>(); unit.SetParent(tile); unit.SetData(level); return(unit); }
void Start() { int[] targets = { 1, 2, 3, 4 }; float[] probabilities = { 0.7f, 0.2f, 0.08f, 0.02f }; int[] times = { 0, 0, 0, 0 }; for (int i = 0; i < _times; i++) { int result = RandomController.Range(targets, probabilities); times[result - 1]++; } for (int i = 0; i < targets.Length; i++) { print(targets[i] + " 出现了 " + times[i] + " 次" + ", 概率为 " + (float)times[i] / _times * 100 + "%"); } }
// 创建Unit private Unit CreateUnit(Tile tile, int level = -1) { int type = RandomController.Range(GlobalValue.UnitClassList, GlobalValue.UnitSpawnProbList); Unit unit = null; switch (type) { case 1: unit = CreateNormalUnit(tile, level); break; case 2: unit = CreateMovableUnit(tile); break; default: print("Wrong type: " + type); break; } return(unit); }