暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoadingSpinner.cs 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. mSpinner = new VisualElement();
  13. Add(mSpinner);
  14. mSpinner.style.backgroundImage = Images.GetImage(Images.Name.Loading);
  15. mSpinner.style.position = Position.Absolute;
  16. mSpinner.style.width = 16;
  17. mSpinner.style.height = 16;
  18. mSpinner.style.left = -8;
  19. mSpinner.style.top = -8;
  20. style.position = Position.Relative;
  21. style.width = 16;
  22. style.height = 16;
  23. style.left = 8;
  24. style.top = 8;
  25. }
  26. internal void Dispose()
  27. {
  28. if (mStarted)
  29. EditorApplication.update -= UpdateProgress;
  30. }
  31. internal void Start()
  32. {
  33. if (mStarted)
  34. return;
  35. mRotation = 0;
  36. mLastRotationTime = EditorApplication.timeSinceStartup;
  37. EditorApplication.update += UpdateProgress;
  38. mStarted = true;
  39. }
  40. internal void Stop()
  41. {
  42. if (!mStarted)
  43. return;
  44. EditorApplication.update -= UpdateProgress;
  45. mStarted = false;
  46. }
  47. void UpdateProgress()
  48. {
  49. double currentTime = EditorApplication.timeSinceStartup;
  50. double deltaTime = currentTime - mLastRotationTime;
  51. mSpinner.transform.rotation = Quaternion.Euler(0, 0, mRotation);
  52. mRotation += (int)(ROTATION_SPEED * deltaTime);
  53. mRotation = mRotation % 360;
  54. if (mRotation < 0) mRotation += 360;
  55. mLastRotationTime = currentTime;
  56. }
  57. int mRotation;
  58. double mLastRotationTime;
  59. bool mStarted;
  60. VisualElement mSpinner;
  61. const int ROTATION_SPEED = 360; // Euler degrees per second
  62. }
  63. }