Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SampleFactory.cs 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class SampleFactory : MonoBehaviour
  4. {
  5. public GameObject parent;
  6. public GameObject prefab;
  7. public int LimitCount = 2000;
  8. public Text spawnedObjectsCounter;
  9. public bool RunTest = true;
  10. public int internalObjs = 0;
  11. public float spawnAmount = 0.1f;
  12. private float spawnPool = 0;
  13. private Vector3 Min = new Vector3(-20, 0, -20);
  14. private Vector3 Max = new Vector3(20, 0, 20);
  15. private GameObject[] spawnedObjects = new GameObject[2000];
  16. void Update()
  17. {
  18. if (RunTest && Time.timeScale != 0.0)
  19. {
  20. if (internalObjs == LimitCount)
  21. {
  22. return;
  23. }
  24. else if (internalObjs < LimitCount)
  25. {
  26. SpawnObject();
  27. }
  28. else
  29. {
  30. DestroyObject();
  31. }
  32. }
  33. spawnedObjectsCounter.text = internalObjs.ToString();
  34. }
  35. void DestroyObject()
  36. {
  37. internalObjs--;
  38. Destroy(spawnedObjects[internalObjs]);
  39. spawnedObjects[internalObjs] = null;
  40. }
  41. void SpawnObject()
  42. {
  43. if (spawnAmount < 1 && spawnPool < 1)
  44. {
  45. spawnPool += spawnAmount;
  46. return;
  47. }
  48. for (int i = 0; i < spawnAmount; i++)
  49. {
  50. var _xAxis = UnityEngine.Random.Range(Min.x, Max.x);
  51. var _yAxis = UnityEngine.Random.Range(Min.y, Max.y);
  52. var _zAxis = UnityEngine.Random.Range(Min.z, Max.z);
  53. var _randomPosition = new Vector3(_xAxis, _yAxis, _zAxis);
  54. spawnedObjects[internalObjs] = Instantiate(prefab, _randomPosition, parent.transform.rotation);
  55. internalObjs++;
  56. if (spawnPool > 1) spawnPool -= 1;
  57. }
  58. }
  59. public void FlushObjects()
  60. {
  61. for (int i = internalObjs; i > 0; i--)
  62. {
  63. DestroyObject();
  64. }
  65. }
  66. }