HotReloadBurstJobsDemo.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #if ENABLE_MONO && (DEVELOPMENT_BUILD || UNITY_EDITOR)
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Jobs;
  6. using UnityEngine.UI;
  7. namespace SingularityGroup.HotReload.Demo {
  8. public class HotReloadBurstJobsDemo : MonoBehaviour {
  9. public Transform[] cubes;
  10. public Text informationText;
  11. public Button openWindowButton;
  12. public Button openScriptButton;
  13. public TextAsset thisScript;
  14. TransformAccessArray cubeTransforms;
  15. CubeJob job;
  16. void Awake() {
  17. cubeTransforms = new TransformAccessArray(cubes);
  18. if(Application.isEditor) {
  19. openWindowButton.onClick.AddListener(Demo.I.OpenHotReloadWindow);
  20. openScriptButton.onClick.AddListener(() => Demo.I.OpenScriptFile(thisScript, 49, 17));
  21. } else {
  22. openWindowButton.gameObject.SetActive(false);
  23. openScriptButton.gameObject.SetActive(false);
  24. }
  25. informationText.gameObject.SetActive(true);
  26. }
  27. void Update() {
  28. job.deltaTime = Time.deltaTime;
  29. job.time = Time.time;
  30. var handle = job.Schedule(cubeTransforms);
  31. handle.Complete();
  32. if (Demo.I.IsServerRunning()) {
  33. informationText.text = "Hot Reload is running";
  34. } else {
  35. informationText.text = "Hot Reload is not running";
  36. }
  37. }
  38. struct CubeJob : IJobParallelForTransform {
  39. public float deltaTime;
  40. public float time;
  41. public void Execute(int index, TransformAccess transform) {
  42. transform.localRotation *= Quaternion.Euler(50 * deltaTime, 0, 0);
  43. // Uncomment this code to scale the cubes
  44. // var scale = Mathf.Abs(Mathf.Sin(time));
  45. // transform.localScale = new Vector3(scale, scale, scale);
  46. // Uncomment this code to make the cube move from left to right and back
  47. // transform.position += (transform.localScale.x < 0.5 ? Vector3.left : Vector3.right) * deltaTime;
  48. }
  49. }
  50. void OnDestroy() {
  51. cubeTransforms.Dispose();
  52. }
  53. }
  54. }
  55. #endif