ProgressIndicatorController.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Threading.Tasks;
  2. using UnityEngine;
  3. namespace SC.XR.Unity
  4. {
  5. public class ProgressIndicatorController : MonoBehaviour
  6. {
  7. [SerializeField, Header("Indicators")]
  8. protected GameObject progressIndicatorLoadingBarGo = null;
  9. [SerializeField]
  10. protected GameObject progressIndicatorLoadingOrbGo = null;
  11. [SerializeField, Range(0f, 1f)]
  12. public float targetProgress = 0f;
  13. protected IProgressIndicator progressIndicatorLoadingBar;
  14. protected IProgressIndicator progressIndicatorLoadingOrb;
  15. private void OnEnable()
  16. {
  17. progressIndicatorLoadingBar = progressIndicatorLoadingBarGo.GetComponent<IProgressIndicator>();
  18. progressIndicatorLoadingOrb = progressIndicatorLoadingOrbGo.GetComponent<IProgressIndicator>();
  19. }
  20. public void Update()
  21. {
  22. if (Input.GetKeyDown(KeyCode.Space))
  23. {
  24. HandleButtonClick(progressIndicatorLoadingBar);
  25. HandleButtonClick(progressIndicatorLoadingOrb);
  26. }
  27. progressIndicatorLoadingBar.SetProgress(targetProgress);
  28. progressIndicatorLoadingOrb.SetProgress(targetProgress);
  29. }
  30. protected async void HandleButtonClick(IProgressIndicator indicator)
  31. {
  32. // If the indicator is opening or closing, wait for that to finish before trying to open / close it
  33. // Otherwise the indicator will display an error and take no action
  34. await indicator.AwaitTransitionAsync();
  35. switch (indicator.State)
  36. {
  37. case ProgressIndicatorState.Closed:
  38. await indicator.OpenAsync();
  39. break;
  40. case ProgressIndicatorState.Open:
  41. await indicator.CloseAsync();
  42. break;
  43. }
  44. }
  45. //private async void OpenProgressIndicator(IProgressIndicator indicator)
  46. //{
  47. // await indicator.OpenAsync();
  48. // float timeStarted = Time.time;
  49. // while (Time.time < timeStarted + loadingTime)
  50. // {
  51. // float normalizedProgress = Mathf.Clamp01((Time.time - timeStarted) / loadingTime);
  52. // indicator.Progress = normalizedProgress;
  53. // await Task.Yield();
  54. // switch (indicator.State)
  55. // {
  56. // case ProgressIndicatorState.Open:
  57. // break;
  58. // default:
  59. // // The indicator was closed
  60. // return;
  61. // }
  62. // }
  63. // await indicator.CloseAsync();
  64. //}
  65. }
  66. }