Files
PrinceOfGlory/Assets/Scripts/XRCore/GameModel/GameModeMgr.cs
kridoo 333a4f3ecb 1
2025-12-09 11:28:02 +08:00

130 lines
3.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using BigSpace.XRCore.Base;
using UnityEngine;
using UnityEngine.Video;
namespace BigSpace.XRCore.GameMode
{
public class GameModeMgr : MonoSingleton<GameModeMgr>
{
private IGameMode _gameMode;
[SerializeField]private bool _isOffline = true;
public bool IsOffline => _isOffline;
public int NextSceneId = 10000;
[SerializeField] private VideoPlayer videoPlayer;
private bool initialized;
private bool videoHidden;
private void Start()
{
//Application.targetFrameRate = 30;
SceneMgr.Instance.nowSceneId = NextSceneId;
StartCoroutine(PlayVideoThenInitGameMode());
//PicoApi.InitEnterpriseService();
}
private IEnumerator PlayVideoThenInitGameMode()
{
if (videoPlayer == null && !TryGetComponent(out videoPlayer))
{
Debug.Log("[GameModeMgr] No VideoPlayer, init directly.");
InitGameMode();
yield break;
}
videoPlayer.loopPointReached += OnVideoFinished;
videoPlayer.errorReceived += OnVideoError;
Debug.Log("[GameModeMgr] Video events registered, preparing.");
if (!videoPlayer.isPrepared)
{
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
}
Debug.Log("[GameModeMgr] Video prepared, play.");
videoPlayer.Play();
float waitToStart = 2f;
float elapsed = 0f;
while (!videoPlayer.isPlaying && elapsed < waitToStart)
{
elapsed += Time.deltaTime;
yield return null;
}
float maxDuration = videoPlayer.clip != null ? (float)videoPlayer.clip.length + 1f : 5f;
elapsed = 0f;
while (videoPlayer.isPlaying && elapsed < maxDuration)
{
elapsed += Time.deltaTime;
yield return null;
}
Debug.Log("[GameModeMgr] Video finished by coroutine.");
InitGameMode();
}
private void OnVideoFinished(VideoPlayer _)
{
Debug.Log("[GameModeMgr] Video finished event.");
InitGameMode();
}
private void OnVideoError(VideoPlayer _, string __)
{
Debug.LogWarning("[GameModeMgr] Video error, init directly.");
InitGameMode();
}
private void HideVideoPlayer()
{
if (videoHidden || videoPlayer == null)
{
return;
}
videoHidden = true;
try
{
if (videoPlayer.isPlaying)
{
videoPlayer.Stop();
}
videoPlayer.gameObject.SetActive(false);
}
catch
{
}
}
private void InitGameMode()
{
if (initialized)
{
return;
}
initialized = true;
HideVideoPlayer();
if (videoPlayer != null)
{
videoPlayer.loopPointReached -= OnVideoFinished;
videoPlayer.errorReceived -= OnVideoError;
}
_gameMode = new OfflineMode();
_gameMode.Initialize();
}
}
}