OverlayManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // UnityEngine.UI was moved to a package in 2019.2.0
  2. // Unfortunately no way to test for this across all Unity versions yet
  3. // You can set up the asmdef to reference the new package, but the package doesn't
  4. // existing in Unity 2017 etc, and it throws an error due to missing reference
  5. #define AVPRO_PACKAGE_UNITYUI
  6. #if (UNITY_2019_2_OR_NEWER && AVPRO_PACKAGE_UNITYUI) || (!UNITY_2019_2_OR_NEWER)
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. //-----------------------------------------------------------------------------
  10. // Copyright 2018-2021 RenderHeads Ltd. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. namespace RenderHeads.Media.AVProVideo.Demos.UI
  13. {
  14. public class OverlayManager : MonoBehaviour
  15. {
  16. public enum Feedback
  17. {
  18. Play,
  19. Pause,
  20. SeekForward,
  21. SeekBack,
  22. VolumeUp,
  23. VolumeDown,
  24. VolumeMute,
  25. }
  26. [SerializeField] Image _stalledImage = null;
  27. [SerializeField] Image _feedbackImage = null;
  28. [SerializeField] CanvasGroup _feedbackCanvas = null;
  29. [SerializeField] float _startScale = 0.25f;
  30. [SerializeField] float _endScale = 1.0f;
  31. [SerializeField] float _animationSpeed = 1.5f;
  32. private Material _feedbackMaterial;
  33. private float _feedbackTimer;
  34. private readonly LazyShaderProperty _propMute = new LazyShaderProperty("_Mute");
  35. private readonly LazyShaderProperty _propVolume = new LazyShaderProperty("_Volume");
  36. void Start()
  37. {
  38. _feedbackMaterial = new Material(_feedbackImage.material);
  39. _feedbackImage.material = _feedbackMaterial;
  40. _feedbackCanvas.alpha = 0f;
  41. _feedbackTimer = 1f;
  42. }
  43. void OnDestroy()
  44. {
  45. if (_feedbackMaterial)
  46. {
  47. Material.Destroy(_feedbackMaterial);
  48. _feedbackMaterial = null;
  49. }
  50. }
  51. public void Reset()
  52. {
  53. _stalledImage.enabled = false;
  54. }
  55. public void TriggerStalled()
  56. {
  57. _stalledImage.enabled = true;
  58. }
  59. private const string KeywordPlay = "UI_PLAY";
  60. private const string KeywordPause = "UI_PAUSE";
  61. private const string KeywordSeekBack = "UI_BACK";
  62. private const string KeywordSeekForward = "UI_FORWARD";
  63. private const string KeywordVolume = "UI_VOLUME";
  64. private const string KeywordVolumeUp = "UI_VOLUMEUP";
  65. private const string KeywordVolumeDown = "UI_VOLUMEDOWN";
  66. private const string KeywordVolumeMute = "UI_VOLUMEMUTE";
  67. public void TriggerFeedback(Feedback feedback)
  68. {
  69. _feedbackMaterial.DisableKeyword(KeywordPlay);
  70. _feedbackMaterial.DisableKeyword(KeywordPause);
  71. _feedbackMaterial.DisableKeyword(KeywordSeekBack);
  72. _feedbackMaterial.DisableKeyword(KeywordSeekForward);
  73. _feedbackMaterial.DisableKeyword(KeywordVolume);
  74. _feedbackMaterial.DisableKeyword(KeywordVolumeUp);
  75. _feedbackMaterial.DisableKeyword(KeywordVolumeDown);
  76. _feedbackMaterial.DisableKeyword(KeywordVolumeMute);
  77. string keyword = null;
  78. switch (feedback)
  79. {
  80. case Feedback.Play:
  81. keyword = KeywordPlay;
  82. break;
  83. case Feedback.Pause:
  84. keyword = KeywordPause;
  85. break;
  86. case Feedback.SeekBack:
  87. keyword = KeywordSeekBack;
  88. break;
  89. case Feedback.SeekForward:
  90. keyword = KeywordSeekForward;
  91. break;
  92. case Feedback.VolumeUp:
  93. keyword = KeywordVolume;
  94. _feedbackMaterial.SetFloat(_propMute.Id, 0f);
  95. _feedbackMaterial.SetFloat(_propVolume.Id, 1f);
  96. break;
  97. case Feedback.VolumeDown:
  98. keyword = KeywordVolume;
  99. _feedbackMaterial.SetFloat(_propMute.Id, 0f);
  100. _feedbackMaterial.SetFloat(_propVolume.Id, 0.5f);
  101. break;
  102. case Feedback.VolumeMute:
  103. keyword = KeywordVolume;
  104. _feedbackMaterial.SetFloat(_propVolume.Id, 1f);
  105. _feedbackMaterial.SetFloat(_propMute.Id, 1f);
  106. break;
  107. }
  108. if (!string.IsNullOrEmpty(keyword))
  109. {
  110. _feedbackMaterial.EnableKeyword(keyword);
  111. }
  112. _feedbackCanvas.alpha = 1f;
  113. _feedbackCanvas.transform.localScale = new Vector3(_startScale, _startScale, _startScale);
  114. _feedbackTimer = 0f;
  115. Update();
  116. }
  117. private void Update()
  118. {
  119. // Update scaling and fading
  120. float t2 = Mathf.Clamp01(_feedbackTimer);
  121. float t = Mathf.Clamp01((_feedbackTimer - 0.5f) * 2f);
  122. _feedbackCanvas.alpha = Mathf.Lerp(1f, 0f, PowerEaseOut(t, 1f));
  123. if (_feedbackCanvas.alpha > 0f)
  124. {
  125. float scale = Mathf.Lerp(_startScale, _endScale, PowerEaseOut(t2, 2f));
  126. _feedbackCanvas.transform.localScale = new Vector3(scale, scale, scale);
  127. }
  128. _feedbackTimer += Time.deltaTime * _animationSpeed;
  129. }
  130. private static float PowerEaseOut(float t, float power)
  131. {
  132. return 1f - Mathf.Abs(Mathf.Pow(t - 1f, power));
  133. }
  134. }
  135. }
  136. #endif