RunnerSpawnerControlBehaviour.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Unity.Entities;
  6. namespace GPUECSAnimationBaker.Samples.SampleScenes.Marathon.RunnerSystems
  7. {
  8. public class RunnerSpawnerControlBehaviour : MonoBehaviour
  9. {
  10. public Scrollbar fieldSizeXScrollbar;
  11. public Scrollbar fieldSizeZScrollbar;
  12. public Scrollbar nbrOfEntitiesScrollbar;
  13. public TextMeshProUGUI fieldSizeXText;
  14. public TextMeshProUGUI fieldSizeZText;
  15. public TextMeshProUGUI nbrOfEntitiesText;
  16. private IEnumerator Start()
  17. {
  18. while(World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(
  19. typeof(RunnerSpawnerUpdateComponent)).CalculateEntityCount() == 0)
  20. yield return null;
  21. ScrollUpdate();
  22. }
  23. public void ScrollUpdate()
  24. {
  25. RunnerSpawnerControlSystem runnerSpawnerControlSystem =
  26. World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<RunnerSpawnerControlSystem>();
  27. runnerSpawnerControlSystem.doScrollUpdate = true;
  28. runnerSpawnerControlSystem.fieldSizeX = fieldSizeXScrollbar.value * 1000f;
  29. fieldSizeXText.text = runnerSpawnerControlSystem.fieldSizeX.ToString("#.##");
  30. runnerSpawnerControlSystem.fieldSizeZ = fieldSizeZScrollbar.value * 1000f;
  31. fieldSizeZText.text = runnerSpawnerControlSystem.fieldSizeZ.ToString("#.##");
  32. runnerSpawnerControlSystem.nbrOfEntities = Mathf.RoundToInt(nbrOfEntitiesScrollbar.value * 100000);
  33. nbrOfEntitiesText.text = runnerSpawnerControlSystem.nbrOfEntities.ToString();
  34. }
  35. }
  36. public partial class RunnerSpawnerControlSystem : SystemBase
  37. {
  38. public bool doScrollUpdate;
  39. public float fieldSizeX;
  40. public float fieldSizeZ;
  41. public int nbrOfEntities;
  42. protected override void OnUpdate()
  43. {
  44. if (doScrollUpdate)
  45. {
  46. doScrollUpdate = false;
  47. Entities.ForEach((ref RunnerSpawnerUpdateComponent runnerSpawnerUpdate) =>
  48. {
  49. runnerSpawnerUpdate.fieldSizeX = fieldSizeX;
  50. runnerSpawnerUpdate.fieldSizeZ = fieldSizeZ;
  51. runnerSpawnerUpdate.nbrOfRunners = nbrOfEntities;
  52. runnerSpawnerUpdate.updateTime = 0.5f;
  53. }).WithoutBurst().Run();
  54. }
  55. }
  56. }
  57. }