Petition-to-the-Gods-V3/Assets/Scripts/Server/ServerWorkMessageReplyExitPanel.cs
2025-11-19 19:30:13 +08:00

121 lines
3.2 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.Video;
public class ServerWorkMessageReplyExitPanel : MonoBehaviour
{
public static ServerWorkMessageReplyExitPanel Instance;
[Header("影片播放")]
public VideoPlayer videoPlayer;
public RawImage videoDisplay;
private bool clientResponseReceived = false; // ✅ 新增
private bool videoHasFinished = false; // ✅ 新增
private void Awake()
{
Instance = this;
}
void Start()
{
SetupVideoPlayer();
}
public void play()
{
if (videoPlayer != null)
{
Debug.Log("SERVER: 開始播放影片");
clientResponseReceived = false; // ✅ 重置狀態
videoHasFinished = false; // ✅ 重置狀態
videoPlayer.Play();
}
}
public void stop()
{
if (videoPlayer != null)
{
Debug.Log("SERVER: 停止播放影片");
videoPlayer.Stop();
}
}
// ✅ 新增:當收到 CLIENT 訊號時呼叫此方法
public void OnClientResponseReceived()
{
Debug.Log("SERVER: 收到 CLIENT 訊號");
clientResponseReceived = true;
// 如果影片已經播完,立即跳轉
if (videoHasFinished)
{
Debug.Log("SERVER: 影片已播完,立即跳轉");
ProcessNextStep();
}
else
{
Debug.Log("SERVER: 影片還在播放,等待播放完成...");
}
}
void SetupVideoPlayer()
{
if (videoPlayer != null)
{
videoPlayer.loopPointReached += OnVideoFinished; // ✅ 這行現在不會報錯了
videoPlayer.isLooping = false;
if (videoDisplay != null)
{
videoPlayer.targetTexture = null;
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
RenderTexture rt = new RenderTexture(1920, 1080, 24);
videoPlayer.targetTexture = rt;
videoDisplay.texture = rt;
}
Debug.Log("SERVER: VideoPlayer 設置完成");
}
else
{
Debug.LogError("SERVER: VideoPlayer 未指定!");
}
}
// ✅ 新增:影片播放完成的事件處理
void OnVideoFinished(VideoPlayer vp)
{
Debug.Log("SERVER: 影片播放完成");
videoHasFinished = true;
// 只有當收到 CLIENT 訊號後才跳轉
if (clientResponseReceived)
{
Debug.Log("SERVER: CLIENT 訊號已收到,準備跳轉");
ProcessNextStep();
}
else
{
Debug.Log("SERVER: 影片播完,等待 CLIENT 訊號...");
}
}
// ✅ 新增:執行跳轉
void ProcessNextStep()
{
if (ServerWorkSceneController.Instance != null)
{
Debug.Log("SERVER: 跳轉到步驟 10");
ServerWorkSceneController.Instance.processStep(10);
}
else
{
Debug.LogError("SERVER: ServerWorkSceneController.Instance 不存在!無法跳轉");
}
}
}