CommonUtils.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. public static class CommonUtils
  7. {
  8. // random distribution
  9. public static int[] GetRandomPrefabIndexes(int numRequired, ref GameObject[] peoplePrefabs)
  10. {
  11. //int[] result = new int[numRequired];
  12. List<int> r = new List<int>();
  13. List<GameObject> prfb = new List<GameObject>(peoplePrefabs);
  14. prfb.Shuffle();
  15. peoplePrefabs = prfb.ToArray();
  16. for (int i = 0, e = 0; i < numRequired; i++)
  17. {
  18. r.Add(e < peoplePrefabs.Length ? e++ : e = 0);
  19. }
  20. r.Shuffle();
  21. return r.ToArray();
  22. //if (peoplePrefabs.Length < 3)
  23. //{
  24. // for (int i = 0; i < result.Length; i++)
  25. // {
  26. // result[i] = UnityEngine.Random.Range(0, peoplePrefabs.Length);
  27. // }
  28. // return result;
  29. //}
  30. //int numToPick = numRequired;
  31. //List<int> tmpPrefabList = new List<int>();
  32. //while (tmpPrefabList.Count < numRequired)
  33. //{
  34. // var rndList = Enumerable.Range(0, peoplePrefabs.Length).ToList();
  35. // rndList.Shuffle();
  36. // tmpPrefabList.AddRange(rndList);
  37. //}
  38. //tmpPrefabList.RemoveRange(numRequired, tmpPrefabList.Count - numRequired);
  39. //for (int leftCount = tmpPrefabList.Count; leftCount > 0; leftCount--)
  40. //{
  41. // float pickValue = (float)numToPick / (float)leftCount;
  42. // if (UnityEngine.Random.value <= pickValue)
  43. // {
  44. // numToPick--;
  45. // result[numToPick] = tmpPrefabList[leftCount - 1];
  46. // if (numToPick == 0) break;
  47. // }
  48. //}
  49. //return result;
  50. }
  51. }