Centralize VideoPanel logic in UIManager to solve reference ambiguity

This commit is contained in:
yangbear
2026-07-10 05:01:29 +08:00
parent 80cb7598e6
commit 0abf59a7d7
3 changed files with 45 additions and 57 deletions
+3 -16
View File
@@ -133,23 +133,10 @@ public class PingClickHandler : MonoBehaviour
}
// 隐藏视频面板
var videoController = FindObjectOfType<VideoController>(true);
if (videoController == null)
var uiMgr = FindObjectOfType<UIManager>();
if (uiMgr != null)
{
var allT = Resources.FindObjectsOfTypeAll<Transform>();
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();
}
}
}
@@ -150,28 +150,10 @@ public class ShoseClickHandler : MonoBehaviour
}
// 显示 VideoPanel 并播放视频
var videoController = FindObjectOfType<VideoController>(true);
if (videoController == null)
var uiMgr = FindObjectOfType<UIManager>();
if (uiMgr != null)
{
var allT = Resources.FindObjectsOfTypeAll<Transform>();
foreach (var t in allT)
{
if (t.name == "VideoPanel" && t.parent != null && t.parent.name == "Canvas")
{
videoController = t.gameObject.AddComponent<VideoController>();
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<VideoController>(true);
if (videoController != null)
var uiMgr = FindObjectOfType<UIManager>();
if (uiMgr != null)
{
videoController.StopVideo();
videoController.gameObject.SetActive(false);
}
else
{
var allT = Resources.FindObjectsOfTypeAll<Transform>();
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");
+36 -4
View File
@@ -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<UnityEngine.Video.VideoPlayer>(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<UnityEngine.Video.VideoPlayer>(true);
if (vp != null) vp.Stop();
_videoPanel.SetActive(false);
}
}
private void UpdateProgressBar()