SteamVR_Status.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using UnityEngine;
  3. public abstract class SteamVR_Status : MonoBehaviour
  4. {
  5. protected abstract void SetAlpha(float a);
  6. private void OnEnable()
  7. {
  8. SteamVR_Utils.Event.Listen(this.message, new SteamVR_Utils.Event.Handler(this.OnEvent));
  9. }
  10. private void OnDisable()
  11. {
  12. SteamVR_Utils.Event.Remove(this.message, new SteamVR_Utils.Event.Handler(this.OnEvent));
  13. }
  14. private void OnEvent(params object[] args)
  15. {
  16. this.status = (bool)args[0];
  17. if (this.status)
  18. {
  19. if (this.mode == SteamVR_Status.Mode.OnTrue)
  20. {
  21. this.timer = this.duration;
  22. }
  23. }
  24. else if (this.mode == SteamVR_Status.Mode.OnFalse)
  25. {
  26. this.timer = this.duration;
  27. }
  28. }
  29. private void Update()
  30. {
  31. if (this.mode == SteamVR_Status.Mode.OnTrue || this.mode == SteamVR_Status.Mode.OnFalse)
  32. {
  33. this.timer -= Time.deltaTime;
  34. if (this.timer < 0f)
  35. {
  36. this.SetAlpha(0f);
  37. }
  38. else
  39. {
  40. float alpha = 1f;
  41. if (this.timer < this.fade)
  42. {
  43. alpha = this.timer / this.fade;
  44. }
  45. if (this.timer > this.duration - this.fade)
  46. {
  47. alpha = Mathf.InverseLerp(this.duration, this.duration - this.fade, this.timer);
  48. }
  49. this.SetAlpha(alpha);
  50. }
  51. }
  52. else
  53. {
  54. bool flag = (this.mode == SteamVR_Status.Mode.WhileTrue && this.status) || (this.mode == SteamVR_Status.Mode.WhileFalse && !this.status);
  55. this.timer = ((!flag) ? Mathf.Max(0f, this.timer - Time.deltaTime) : Mathf.Min(this.fade, this.timer + Time.deltaTime));
  56. this.SetAlpha(this.timer / this.fade);
  57. }
  58. }
  59. public string message;
  60. public float duration;
  61. public float fade;
  62. protected float timer;
  63. protected bool status;
  64. public SteamVR_Status.Mode mode;
  65. public enum Mode
  66. {
  67. OnTrue,
  68. OnFalse,
  69. WhileTrue,
  70. WhileFalse
  71. }
  72. }