| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Video;
- using UnityEngine.SceneManagement;
- namespace MyInfraredInsertcoin
- {
- public class StandbyVideoManager : MonoBehaviour
- {
- public RawImage videoDisplay; // UI上的RawImage
- public VideoPlayer videoPlayer; // VideoPlayer 组件
- public GameObject uiPanel; // UI 界面的根面板
- public float standbyTime = 180f; // 待机时间(秒)
- public RenderTexture renderTexture; // 用于渲染视频的 RenderTexture
- private bool isVideoPlaying = false;
- private float lastInputTime;
- // 控制外部是否启用用户输入检测
- public bool isUserInputEnabled = true;
- private bool isHomeScene = false; // 标记是否在 Home 场景
- public static StandbyVideoManager _ins;
- public static void Create()
- {
- if (_ins) return;
- GameObject o = Instantiate(Resources.Load<GameObject>("StandbyVideoManager"));
- DontDestroyOnLoad(o);
- _ins = o.GetComponent<StandbyVideoManager>();
- // 添加一个父物体
- o.transform.SetParent(ViewMgr.Instance.transform.Find("1").transform);
- CanvasScaler canvasScaler = o.GetComponent<CanvasScaler>();
- if (canvasScaler != null)
- {
- Destroy(canvasScaler);
- }
- Canvas canvas = o.GetComponent<Canvas>();
- canvas.overrideSorting = true;
- RectTransform rectTransform = o.GetComponent<RectTransform>();
- rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级
- rectTransform.anchorMax = Vector2.one; // 右上角对齐父级
- rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移
- rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移
- rectTransform.localScale = Vector3.one; // 确保缩放为 1
- // 激活 videoDisplay 并开始播放视频
- _ins.PlayStandbyVideo();
- }
- void Awake()
- {
- // 订阅场景加载事件
- SceneManager.sceneLoaded += OnSceneLoaded;
- }
- void OnDisable()
- {
- // 取消订阅场景加载事件
- SceneManager.sceneLoaded -= OnSceneLoaded;
- }
- private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
- {
- ResetIdleTimer();
- // 如果加载的场景是 "Home" 场景,标记为 true
- if (scene.name == "Home")
- {
- isHomeScene = true;
- }
- else
- {
- isHomeScene = false;
- }
- }
- void Start()
- {
- // 确保 RawImage 的纹理是设置为 RenderTexture
- videoDisplay.texture = renderTexture;
- // 确保 VideoPlayer 的 targetTexture 正确设置
- videoPlayer.targetTexture = renderTexture;
- videoPlayer.loopPointReached += OnVideoEnd; // 监听视频播放完毕
- videoPlayer.isLooping = true; // 设置视频循环播放
- // 获取视频的宽高比并应用到 AspectRatioFitter
- AspectRatioFitter aspectFitter = videoDisplay.GetComponent<AspectRatioFitter>();
- float aspectRatio = (float)videoPlayer.width / videoPlayer.height;
- aspectFitter.aspectRatio = aspectRatio;
- ResetIdleTimer(); // 初始化计时器
- }
- // 进入待机视频
- public void PlayStandbyVideo()
- {
- uiPanel.SetActive(true); // 显示视频
- videoPlayer.Play();
- isVideoPlaying = true;
- }
- // 停止视频并重置定时器
- public void StopAndReset()
- {
- videoPlayer.Stop();
- uiPanel.SetActive(false); // 隐藏视频
- isVideoPlaying = false;
- ResetIdleTimer();
- }
- void Update()
- {
- // 只有在 Home 场景下才检测待机时间
- if (isHomeScene)
- {
- // 判断是否达到待机时间,进入待机视频
- if (!isVideoPlaying && Time.time - lastInputTime > standbyTime)
- {
- PlayStandbyVideo();
- }
- // 模拟调用 DetectPointerMovement 检测光标移动
- if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
- {
- DetectPointerMovement(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
- }
- // 模拟调用 SetUserInputEnabled 检测按键按下
- //if (Input.anyKeyDown)
- //{
- // StopVideo();
- //}
- }
- }
- // 重置待机计时器
- private void ResetIdleTimer()
- {
- lastInputTime = Time.time;
- }
- // 视频播放结束后自动回到UI
- private void OnVideoEnd(VideoPlayer vp)
- {
- // 仅当视频没有被手动停止时才执行
- if (isVideoPlaying)
- {
- videoPlayer.Play(); // 重新开始播放
- }
- }
- #region 光标移动,触发关闭视频video
- private Vector2 lastPoint = Vector2.zero; // 记录上一次光点位置
- private float movementThreshold = 10f; // 设定的移动触发阈值
- // 检测光点(光标)移动并调用 EnableUserInput
- /// <summary>
- /// 输入当前移动的ui点
- /// </summary>
- /// <param name="currentPoint"></param>
- public void DetectPointerMovement(Vector2 currentPoint)
- {
- if (lastPoint == Vector2.zero)
- {
- lastPoint = currentPoint; // 初始化位置
- return;
- }
- // 计算光点移动的距离
- float distance = Vector2.Distance(lastPoint, currentPoint);
- if (distance > movementThreshold)
- {
- StopVideo();
- lastPoint = currentPoint; // 更新上次光点位置
- }
- }
- /// <summary>
- /// 外部手动调用停止视频
- /// </summary>
- public void StopVideo()
- {
- // 监听退出视频
- if (isVideoPlaying)
- {
- StopAndReset(); // 调用优化后的停止并重置函数
- }
- else {
- ResetIdleTimer();
- }
- }
- #endregion
- }
- }
|