using DG.Tweening; using FairyGUI; using UnityEngine; using UnityEngine.Video; namespace BallKingdomCrush { public static class VideoPlayerHandover { private static VideoPlayer currentPlayer; private static Transform originalParent; private static GLoader originalLoader; /// /// 接管播放器,切换父物体和绑定loader /// public static void TakeOver(VideoPlayer player, Transform newParent, GLoader newLoader) { if (player == null) { Debug.LogWarning("[VideoPlayerHandover] TakeOver: player is null"); return; } currentPlayer = player; // 只记录第一次的父物体和loader(方便后面还原) if (originalParent == null) originalParent = player.transform.parent; if (originalLoader == null && newLoader != null) originalLoader = newLoader; // 切父物体 player.transform.SetParent(newParent, false); // 延迟绑定到新loader显示,确保 RenderTexture 已准备好 DOTween.To(() => 0, _ => { if (player.targetTexture != null && IsValidRenderTexture(player.targetTexture)) { newLoader.texture = new NTexture(player.targetTexture); newLoader.visible = true; Debug.Log($"[VideoPlayerHandover] TakeOver: bound to loader {newLoader.name}, texture size: {player.targetTexture.width}x{player.targetTexture.height}"); } else { Debug.LogWarning("[VideoPlayerHandover] TakeOver: targetTexture invalid or not ready"); } }, 0, 0.05f); // 延迟 0.05s } /// /// 归还播放器到原父物体和loader /// public static void Return() { if (currentPlayer == null) { Debug.LogWarning("[VideoPlayerHandover] Return: currentPlayer is null"); return; } // 切回原父物体 currentPlayer.transform.SetParent(originalParent, false); // 绑定回原loader显示 if (originalLoader != null) { if (currentPlayer.targetTexture != null && IsValidRenderTexture(currentPlayer.targetTexture)) { originalLoader.texture = new NTexture(currentPlayer.targetTexture); originalLoader.visible = true; Debug.Log($"[VideoPlayerHandover] Return: bound back to original loader {originalLoader.name}, texture size: {currentPlayer.targetTexture.width}x{currentPlayer.targetTexture.height}"); } else { Debug.LogWarning("[VideoPlayerHandover] Return: targetTexture invalid or not ready"); } } // 清除引用,下次接管会重新记录 currentPlayer = null; originalParent = null; originalLoader = null; } /// /// RenderTexture 尺寸合法性检查 /// private static bool IsValidRenderTexture(RenderTexture rt) { return rt != null && rt.width > 0 && rt.height > 0; } } }