From 0abf59a7d7c08d0e62acff336bb69df5807366b4 Mon Sep 17 00:00:00 2001 From: yangbear Date: Fri, 10 Jul 2026 05:01:29 +0800 Subject: [PATCH] Centralize VideoPanel logic in UIManager to solve reference ambiguity --- .../Scripts/Interaction/PingClickHandler.cs | 19 ++------ .../Scripts/Interaction/ShoseClickHandler.cs | 43 +++---------------- Assets/Scripts/UI/UIManager.cs | 40 +++++++++++++++-- 3 files changed, 45 insertions(+), 57 deletions(-) diff --git a/Assets/Scripts/Interaction/PingClickHandler.cs b/Assets/Scripts/Interaction/PingClickHandler.cs index 466a0d0..459f981 100644 --- a/Assets/Scripts/Interaction/PingClickHandler.cs +++ b/Assets/Scripts/Interaction/PingClickHandler.cs @@ -133,23 +133,10 @@ public class PingClickHandler : MonoBehaviour } // 隐藏视频面板 - var videoController = FindObjectOfType(true); - if (videoController == null) + var uiMgr = FindObjectOfType(); + if (uiMgr != null) { - var allT = Resources.FindObjectsOfTypeAll(); - foreach (var t in allT) - { - if (t.name == "VideoPanel" && t.parent != null && t.parent.name == "Canvas") - { - t.gameObject.SetActive(false); - break; - } - } - } - else - { - videoController.StopVideo(); - videoController.gameObject.SetActive(false); + uiMgr.HideVideo(); } } } diff --git a/Assets/Scripts/Interaction/ShoseClickHandler.cs b/Assets/Scripts/Interaction/ShoseClickHandler.cs index e7c5dc0..d122685 100644 --- a/Assets/Scripts/Interaction/ShoseClickHandler.cs +++ b/Assets/Scripts/Interaction/ShoseClickHandler.cs @@ -150,28 +150,10 @@ public class ShoseClickHandler : MonoBehaviour } // 显示 VideoPanel 并播放视频 - var videoController = FindObjectOfType(true); - if (videoController == null) + var uiMgr = FindObjectOfType(); + if (uiMgr != null) { - var allT = Resources.FindObjectsOfTypeAll(); - foreach (var t in allT) - { - if (t.name == "VideoPanel" && t.parent != null && t.parent.name == "Canvas") - { - videoController = t.gameObject.AddComponent(); - break; - } - } - } - - if (videoController != null) - { - videoController.gameObject.SetActive(true); - videoController.PlayVideo("tuoXieZi.mp4"); - } - else - { - Debug.LogError("[ShoseClick] 未找到 VideoPanel,无法播放视频"); + uiMgr.ShowVideo("tuoXieZi.mp4"); } } @@ -189,23 +171,10 @@ public class ShoseClickHandler : MonoBehaviour if (nextObject != null) nextObject.SetActive(false); // --- 恢复相机2、隐藏视频并重新播放语音 --- - var videoController = FindObjectOfType(true); - if (videoController != null) + var uiMgr = FindObjectOfType(); + if (uiMgr != null) { - videoController.StopVideo(); - videoController.gameObject.SetActive(false); - } - else - { - var allT = Resources.FindObjectsOfTypeAll(); - foreach (var t in allT) - { - if (t.name == "VideoPanel" && t.parent != null && t.parent.name == "Canvas") - { - t.gameObject.SetActive(false); - break; - } - } + uiMgr.HideVideo(); } var cam2 = GameObject.Find("camera2"); diff --git a/Assets/Scripts/UI/UIManager.cs b/Assets/Scripts/UI/UIManager.cs index a70addc..e4656e3 100644 --- a/Assets/Scripts/UI/UIManager.cs +++ b/Assets/Scripts/UI/UIManager.cs @@ -25,7 +25,7 @@ public class UIManager : MonoBehaviour [SerializeField] private Text _dialogueText; [Header("=== 画中画视频 ===")] - [SerializeField] private VideoController _videoController; + [SerializeField] private GameObject _videoPanel; [Header("=== 仿真导航栏按钮(旧引用 + Navbar)===")] [SerializeField] private Button _btnFullScreen; @@ -58,6 +58,9 @@ public class UIManager : MonoBehaviour private void Start() { Debug.Log("=== UIManager 初始化开始 ==="); + + // 确保视频最开始是隐藏的 + HideVideo(); if (_stepManager == null) { @@ -228,11 +231,40 @@ public class UIManager : MonoBehaviour private void ToggleVideo(string fileName, bool active) { Debug.Log($"UIManager: [ToggleVideo] file={fileName}, active={active}"); - if (_videoController == null) return; if (active && !string.IsNullOrEmpty(fileName)) - _videoController.PlayVideo(fileName); + ShowVideo(fileName); else - _videoController.StopVideo(); + HideVideo(); + } + + public void ShowVideo(string fileName) + { + if (_videoPanel != null) + { + _videoPanel.SetActive(true); + var vp = _videoPanel.GetComponentInChildren(true); + if (vp != null) + { + vp.isLooping = true; + string path = System.IO.Path.Combine(Application.streamingAssetsPath, "Videos", fileName); + vp.url = path; + vp.Play(); + } + else + { + Debug.LogError("UIManager: 在 _videoPanel 中没有找到 VideoPlayer 组件!"); + } + } + } + + public void HideVideo() + { + if (_videoPanel != null) + { + var vp = _videoPanel.GetComponentInChildren(true); + if (vp != null) vp.Stop(); + _videoPanel.SetActive(false); + } } private void UpdateProgressBar()