StandbyVideoManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Video;
  4. using UnityEngine.SceneManagement;
  5. namespace MyInfraredInsertcoin
  6. {
  7. public class StandbyVideoManager : MonoBehaviour
  8. {
  9. public RawImage videoDisplay; // UI上的RawImage
  10. public VideoPlayer videoPlayer; // VideoPlayer 组件
  11. public GameObject uiPanel; // UI 界面的根面板
  12. public float standbyTime = 180f; // 待机时间(秒)
  13. public RenderTexture renderTexture; // 用于渲染视频的 RenderTexture
  14. private bool isVideoPlaying = false;
  15. private float lastInputTime;
  16. // 控制外部是否启用用户输入检测
  17. public bool isUserInputEnabled = true;
  18. private bool isHomeScene = false; // 标记是否在 Home 场景
  19. public static StandbyVideoManager _ins;
  20. public static void Create()
  21. {
  22. if (_ins) return;
  23. GameObject o = Instantiate(Resources.Load<GameObject>("StandbyVideoManager"));
  24. DontDestroyOnLoad(o);
  25. _ins = o.GetComponent<StandbyVideoManager>();
  26. // 添加一个父物体
  27. o.transform.SetParent(ViewMgr.Instance.transform.Find("1").transform);
  28. CanvasScaler canvasScaler = o.GetComponent<CanvasScaler>();
  29. if (canvasScaler != null)
  30. {
  31. Destroy(canvasScaler);
  32. }
  33. Canvas canvas = o.GetComponent<Canvas>();
  34. canvas.overrideSorting = true;
  35. RectTransform rectTransform = o.GetComponent<RectTransform>();
  36. rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级
  37. rectTransform.anchorMax = Vector2.one; // 右上角对齐父级
  38. rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移
  39. rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移
  40. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  41. // 激活 videoDisplay 并开始播放视频
  42. _ins.PlayStandbyVideo();
  43. }
  44. void Awake()
  45. {
  46. // 订阅场景加载事件
  47. SceneManager.sceneLoaded += OnSceneLoaded;
  48. }
  49. void OnDisable()
  50. {
  51. // 取消订阅场景加载事件
  52. SceneManager.sceneLoaded -= OnSceneLoaded;
  53. }
  54. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  55. {
  56. ResetIdleTimer();
  57. // 如果加载的场景是 "Home" 场景,标记为 true
  58. if (scene.name == "Home")
  59. {
  60. isHomeScene = true;
  61. }
  62. else
  63. {
  64. isHomeScene = false;
  65. }
  66. }
  67. void Start()
  68. {
  69. // 确保 RawImage 的纹理是设置为 RenderTexture
  70. videoDisplay.texture = renderTexture;
  71. // 确保 VideoPlayer 的 targetTexture 正确设置
  72. videoPlayer.targetTexture = renderTexture;
  73. videoPlayer.loopPointReached += OnVideoEnd; // 监听视频播放完毕
  74. videoPlayer.isLooping = true; // 设置视频循环播放
  75. // 获取视频的宽高比并应用到 AspectRatioFitter
  76. AspectRatioFitter aspectFitter = videoDisplay.GetComponent<AspectRatioFitter>();
  77. float aspectRatio = (float)videoPlayer.width / videoPlayer.height;
  78. aspectFitter.aspectRatio = aspectRatio;
  79. ResetIdleTimer(); // 初始化计时器
  80. }
  81. // 进入待机视频
  82. public void PlayStandbyVideo()
  83. {
  84. uiPanel.SetActive(true); // 显示视频
  85. videoPlayer.Play();
  86. isVideoPlaying = true;
  87. }
  88. // 停止视频并重置定时器
  89. public void StopAndReset()
  90. {
  91. videoPlayer.Stop();
  92. uiPanel.SetActive(false); // 隐藏视频
  93. isVideoPlaying = false;
  94. ResetIdleTimer();
  95. }
  96. void Update()
  97. {
  98. // 只有在 Home 场景下才检测待机时间
  99. if (isHomeScene)
  100. {
  101. // 判断是否达到待机时间,进入待机视频
  102. if (!isVideoPlaying && Time.time - lastInputTime > standbyTime)
  103. {
  104. PlayStandbyVideo();
  105. }
  106. // 模拟调用 DetectPointerMovement 检测光标移动
  107. if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
  108. {
  109. DetectPointerMovement(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
  110. }
  111. // 模拟调用 SetUserInputEnabled 检测按键按下
  112. //if (Input.anyKeyDown)
  113. //{
  114. // StopVideo();
  115. //}
  116. }
  117. }
  118. // 重置待机计时器
  119. private void ResetIdleTimer()
  120. {
  121. lastInputTime = Time.time;
  122. }
  123. // 视频播放结束后自动回到UI
  124. private void OnVideoEnd(VideoPlayer vp)
  125. {
  126. // 仅当视频没有被手动停止时才执行
  127. if (isVideoPlaying)
  128. {
  129. videoPlayer.Play(); // 重新开始播放
  130. }
  131. }
  132. #region 光标移动,触发关闭视频video
  133. private Vector2 lastPoint = Vector2.zero; // 记录上一次光点位置
  134. private float movementThreshold = 10f; // 设定的移动触发阈值
  135. // 检测光点(光标)移动并调用 EnableUserInput
  136. /// <summary>
  137. /// 输入当前移动的ui点
  138. /// </summary>
  139. /// <param name="currentPoint"></param>
  140. public void DetectPointerMovement(Vector2 currentPoint)
  141. {
  142. if (lastPoint == Vector2.zero)
  143. {
  144. lastPoint = currentPoint; // 初始化位置
  145. return;
  146. }
  147. // 计算光点移动的距离
  148. float distance = Vector2.Distance(lastPoint, currentPoint);
  149. if (distance > movementThreshold)
  150. {
  151. StopVideo();
  152. lastPoint = currentPoint; // 更新上次光点位置
  153. }
  154. }
  155. /// <summary>
  156. /// 外部手动调用停止视频
  157. /// </summary>
  158. public void StopVideo()
  159. {
  160. // 监听退出视频
  161. if (isVideoPlaying)
  162. {
  163. StopAndReset(); // 调用优化后的停止并重置函数
  164. }
  165. else {
  166. ResetIdleTimer();
  167. }
  168. }
  169. #endregion
  170. }
  171. }