Callout.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace Unity.VRTemplate
  4. {
  5. /// <summary>
  6. /// Callout used to display information like world and controller tooltips.
  7. /// </summary>
  8. public class Callout : MonoBehaviour
  9. {
  10. [SerializeField]
  11. [Tooltip("The tooltip Transform associated with this Callout.")]
  12. Transform m_LazyTooltip;
  13. [SerializeField]
  14. [Tooltip("The line curve GameObject associated with this Callout.")]
  15. GameObject m_Curve;
  16. [SerializeField]
  17. [Tooltip("The required time to dwell on this callout before the tooltip and curve are enabled.")]
  18. float m_DwellTime = 1f;
  19. [SerializeField]
  20. [Tooltip("Whether the associated tooltip will be unparented on Start.")]
  21. bool m_Unparent = true;
  22. [SerializeField]
  23. [Tooltip("Whether the associated tooltip and curve will be disabled on Start.")]
  24. bool m_TurnOffAtStart = true;
  25. bool m_Gazing = false;
  26. Coroutine m_StartCo;
  27. Coroutine m_EndCo;
  28. void Start()
  29. {
  30. if (m_Unparent)
  31. {
  32. if (m_LazyTooltip != null)
  33. m_LazyTooltip.SetParent(null);
  34. }
  35. if (m_TurnOffAtStart)
  36. {
  37. if (m_LazyTooltip != null)
  38. m_LazyTooltip.gameObject.SetActive(false);
  39. if (m_Curve != null)
  40. m_Curve.SetActive(false);
  41. }
  42. }
  43. public void GazeHoverStart()
  44. {
  45. m_Gazing = true;
  46. if (m_StartCo != null)
  47. StopCoroutine(m_StartCo);
  48. if (m_EndCo != null)
  49. StopCoroutine(m_EndCo);
  50. m_StartCo = StartCoroutine(StartDelay());
  51. }
  52. public void GazeHoverEnd()
  53. {
  54. m_Gazing = false;
  55. m_EndCo = StartCoroutine(EndDelay());
  56. }
  57. IEnumerator StartDelay()
  58. {
  59. yield return new WaitForSeconds(m_DwellTime);
  60. if (m_Gazing)
  61. TurnOnStuff();
  62. }
  63. IEnumerator EndDelay()
  64. {
  65. if (!m_Gazing)
  66. TurnOffStuff();
  67. yield return null;
  68. }
  69. void TurnOnStuff()
  70. {
  71. if (m_LazyTooltip != null)
  72. m_LazyTooltip.gameObject.SetActive(true);
  73. if (m_Curve != null)
  74. m_Curve.SetActive(true);
  75. }
  76. void TurnOffStuff()
  77. {
  78. if (m_LazyTooltip != null)
  79. m_LazyTooltip.gameObject.SetActive(false);
  80. if (m_Curve != null)
  81. m_Curve.SetActive(false);
  82. }
  83. }
  84. }