ProgressIndicatorBar.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Threading.Tasks;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace SC.XR.Unity
  5. {
  6. public class ProgressIndicatorBar : MonoBehaviour, IProgressIndicator
  7. {
  8. const float SmoothProgressSpeed = 0.5f;
  9. [SerializeField]
  10. [Range(0f, 1f)]
  11. private float progress = 0f;
  12. public ProgressIndicatorState State { get { return state; } }
  13. public float Progress { get { return progress; } set { if (value >= 0 && value <= 1) { progress = value; } } }
  14. // The animated progress bar object
  15. [SerializeField]
  16. private Transform progressBar = null;
  17. // The progress text used by all non-'None' progress styles
  18. [SerializeField]
  19. private Text progressText = null;
  20. // Whether to display the percentage as text in addition to the loading bar
  21. [SerializeField]
  22. private bool displayPercentage = true;
  23. private float smoothProgress = 0f;
  24. private float lastSmoothProgress = -1;
  25. private ProgressIndicatorState state = ProgressIndicatorState.Closed;
  26. private Vector3 barScale = Vector3.one;
  27. public async Task OpenAsync()
  28. {
  29. if (state != ProgressIndicatorState.Closed)
  30. {
  31. throw new System.Exception("Can't open in state " + state);
  32. }
  33. smoothProgress = 0;
  34. lastSmoothProgress = 0;
  35. gameObject.SetActive(true);
  36. state = ProgressIndicatorState.Opening;
  37. await Task.Yield();
  38. state = ProgressIndicatorState.Open;
  39. }
  40. public async Task CloseAsync()
  41. {
  42. if (state != ProgressIndicatorState.Open)
  43. {
  44. throw new System.Exception("Can't close in state " + state);
  45. }
  46. state = ProgressIndicatorState.Closing;
  47. await Task.Yield();
  48. state = ProgressIndicatorState.Closed;
  49. gameObject.SetActive(false);
  50. }
  51. public async Task AwaitTransitionAsync()
  52. {
  53. while (isActiveAndEnabled)
  54. {
  55. switch (state)
  56. {
  57. case ProgressIndicatorState.Open:
  58. case ProgressIndicatorState.Closed:
  59. return;
  60. default:
  61. break;
  62. }
  63. await Task.Yield();
  64. }
  65. }
  66. public void SetProgress(float p)
  67. {
  68. progress = p;
  69. }
  70. private void Update()
  71. {
  72. if (state != ProgressIndicatorState.Open)
  73. {
  74. return;
  75. }
  76. smoothProgress = Mathf.Lerp(smoothProgress, progress, SmoothProgressSpeed);
  77. if (smoothProgress > 0.99f)
  78. {
  79. smoothProgress = 1f;
  80. }
  81. barScale.x = Mathf.Clamp(smoothProgress, 0.01f, 1f);
  82. progressBar.localScale = barScale;
  83. progressText.gameObject.SetActive(displayPercentage);
  84. if (lastSmoothProgress != smoothProgress)
  85. {
  86. progressText.text = string.Format("{0:P0}", smoothProgress);
  87. lastSmoothProgress = smoothProgress;
  88. }
  89. }
  90. }
  91. }