LoadingSpinner.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.UIElements;
  4. namespace Unity.PlasticSCM.Editor.UI.UIElements
  5. {
  6. internal class LoadingSpinner : VisualElement
  7. {
  8. internal LoadingSpinner()
  9. {
  10. mStarted = false;
  11. // add child elements to set up centered spinner rotation
  12. VisualElement spinner = new VisualElement();
  13. Add(spinner);
  14. spinner.style.backgroundImage = Images.GetImage(Images.Name.Loading);
  15. spinner.style.width = 16;
  16. spinner.style.height = 16;
  17. spinner.style.left = -8;
  18. spinner.style.top = -8;
  19. style.position = Position.Relative;
  20. style.width = 16;
  21. style.height = 16;
  22. style.left = 8;
  23. style.top = 8;
  24. }
  25. internal void Dispose()
  26. {
  27. if (mStarted)
  28. EditorApplication.update -= UpdateProgress;
  29. }
  30. internal void Start()
  31. {
  32. if (mStarted)
  33. return;
  34. mRotation = 0;
  35. mLastRotationTime = EditorApplication.timeSinceStartup;
  36. EditorApplication.update += UpdateProgress;
  37. mStarted = true;
  38. }
  39. internal void Stop()
  40. {
  41. if (!mStarted)
  42. return;
  43. EditorApplication.update -= UpdateProgress;
  44. mStarted = false;
  45. }
  46. void UpdateProgress()
  47. {
  48. double currentTime = EditorApplication.timeSinceStartup;
  49. double deltaTime = currentTime - mLastRotationTime;
  50. transform.rotation = Quaternion.Euler(0, 0, mRotation);
  51. mRotation += (int)(ROTATION_SPEED * deltaTime);
  52. mRotation = mRotation % 360;
  53. if (mRotation < 0) mRotation += 360;
  54. mLastRotationTime = currentTime;
  55. }
  56. int mRotation;
  57. double mLastRotationTime;
  58. bool mStarted;
  59. const int ROTATION_SPEED = 360; // Euler degrees per second
  60. }
  61. }