98 lines
2.9 KiB
C#
98 lines
2.9 KiB
C#
using System.Collections;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.Video;
|
||
|
||
public class ClientGameBackToRealSceneController : MonoBehaviour
|
||
{
|
||
public static ClientGameBackToRealSceneController Instance;
|
||
|
||
public VideoPlayer videoPlayer;
|
||
public RawImage videoDisplay;
|
||
|
||
// --- 【新增變數】用於追蹤兩個條件是否滿足 ---
|
||
private bool isVideoFinished = false;
|
||
private bool isSignalReceived = false;
|
||
|
||
private void Awake()
|
||
{
|
||
Instance = this;
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
SetupVideoPlayer();
|
||
|
||
// 開始播放影片
|
||
if (videoPlayer != null)
|
||
{
|
||
videoPlayer.Play();
|
||
Debug.Log("開始播放影片");
|
||
}
|
||
}
|
||
|
||
void SetupVideoPlayer()
|
||
{
|
||
if (videoPlayer != null)
|
||
{
|
||
// 設置影片結束事件
|
||
videoPlayer.loopPointReached += OnVideoFinished;
|
||
|
||
// 設置影片顯示 (略)
|
||
if (videoDisplay != null)
|
||
{
|
||
videoPlayer.targetTexture = null;
|
||
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
|
||
RenderTexture rt = new RenderTexture(1080, 1920, 24);
|
||
videoPlayer.targetTexture = rt;
|
||
videoDisplay.texture = rt;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 影片播放結束時呼叫
|
||
void OnVideoFinished(VideoPlayer vp)
|
||
{
|
||
Debug.Log("影片播放完成。");
|
||
isVideoFinished = true; // 影片結束標記設為 true
|
||
CheckAndLoadScene(); // 檢查是否可以跳轉
|
||
}
|
||
|
||
// --- 【新增方法】供 ClientMessageHandler 呼叫,標記訊號已收到 ---
|
||
public void ReceiveSignalFromPlayerB()
|
||
{
|
||
Debug.Log("收到玩家B的訊號 (gameFinalWords)。");
|
||
isSignalReceived = true; // 訊號接收標記設為 true
|
||
CheckAndLoadScene(); // 檢查是否可以跳轉
|
||
}
|
||
|
||
// --- 【新增方法】核心檢查跳轉邏輯 ---
|
||
private void CheckAndLoadScene()
|
||
{
|
||
// 確保 ClientLastWordsSceneController.words 已經有值
|
||
bool hasWords = !string.IsNullOrEmpty(ClientLastWordsSceneController.words);
|
||
|
||
if (isVideoFinished && isSignalReceived && hasWords)
|
||
{
|
||
Debug.Log("🎉 影片和訊號條件滿足,準備跳轉到下一場景...");
|
||
StartCoroutine(LoadNextScene());
|
||
}
|
||
else if (isVideoFinished && !isSignalReceived)
|
||
{
|
||
// 影片已結束,等待訊號
|
||
Debug.Log("影片已結束,正在等待玩家B訊號...");
|
||
}
|
||
else if (!isVideoFinished && isSignalReceived)
|
||
{
|
||
// 訊號已收到,等待影片結束
|
||
Debug.Log("已收到玩家B訊號,正在等待影片播放完成...");
|
||
}
|
||
}
|
||
|
||
public IEnumerator LoadNextScene()
|
||
{
|
||
yield return new WaitForSeconds(0f);
|
||
SceneManager.LoadScene("ClientLastWordsScene");
|
||
}
|
||
} |