160 lines
5.3 KiB
C#
160 lines
5.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using System;
|
|
using Input;
|
|
using Settings;
|
|
|
|
namespace Utility
|
|
{
|
|
public class SceneOrientationEnforcer : MonoBehaviour
|
|
{
|
|
private static SceneOrientationEnforcer _instance;
|
|
private static bool _isQuitting;
|
|
public static SceneOrientationEnforcer Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null && Application.isPlaying && !_isQuitting)
|
|
{
|
|
_instance = FindAnyObjectByType<SceneOrientationEnforcer>();
|
|
if (_instance == null)
|
|
{
|
|
var go = new GameObject("SceneOrientationEnforcer");
|
|
_instance = go.AddComponent<SceneOrientationEnforcer>();
|
|
// DontDestroyOnLoad(go); // Uncomment if you want persistence
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
[Header("Config")]
|
|
public SceneOrientationConfig orientationConfig;
|
|
public GameObject orientationPromptPrefab;
|
|
|
|
public event Action OnOrientationCorrect;
|
|
|
|
private GameObject promptInstance;
|
|
private ScreenOrientationRequirement requiredOrientation;
|
|
private bool orientationCorrect;
|
|
private Coroutine orientationCheckCoroutine;
|
|
|
|
void Awake()
|
|
{
|
|
_instance = this;
|
|
OnOrientationCorrect += HandleOrientationCorrect;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
// Subscribe to sceneLoaded event
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
// Manually invoke for the first scene (unless it's Main Menu)
|
|
var activeScene = SceneManager.GetActiveScene();
|
|
if (!IsMainMenuScene(activeScene))
|
|
{
|
|
OnSceneLoaded(activeScene, LoadSceneMode.Single);
|
|
}
|
|
}
|
|
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
// Clean up any previous prompt/coroutine
|
|
// CleanupPromptAndCoroutine();
|
|
if (IsMainMenuScene(scene))
|
|
return;
|
|
requiredOrientation = orientationConfig != null ? orientationConfig.GetRequirementForScene(scene.name) : ScreenOrientationRequirement.Portrait;
|
|
orientationCorrect = IsOrientationCorrect();
|
|
if (!orientationCorrect)
|
|
{
|
|
InputManager.Instance.SetInputMode(InputMode.UI);
|
|
ShowPrompt();
|
|
orientationCheckCoroutine = StartCoroutine(OrientationCheckRoutine());
|
|
}
|
|
}
|
|
|
|
private bool IsMainMenuScene(Scene scene)
|
|
{
|
|
// Adjust this logic if you have a different main menu scene name
|
|
return scene.name == "Main Menu" || scene.name == "MainMenu";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if the current device orientation matches the required orientation for the scene
|
|
/// </summary>
|
|
/// <returns>True if the orientation is correct, false otherwise</returns>
|
|
public bool IsOrientationCorrect()
|
|
{
|
|
switch (requiredOrientation)
|
|
{
|
|
case ScreenOrientationRequirement.Portrait:
|
|
return Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown;
|
|
case ScreenOrientationRequirement.Landscape:
|
|
return Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private System.Collections.IEnumerator OrientationCheckRoutine()
|
|
{
|
|
while (!IsOrientationCorrect())
|
|
{
|
|
yield return new WaitForSeconds(0.5f);
|
|
}
|
|
orientationCorrect = true;
|
|
OnOrientationCorrect?.Invoke();
|
|
}
|
|
|
|
private void ShowPrompt()
|
|
{
|
|
if (orientationPromptPrefab != null && promptInstance == null)
|
|
{
|
|
promptInstance = Instantiate(orientationPromptPrefab);
|
|
DontDestroyOnLoad(promptInstance);
|
|
}
|
|
}
|
|
|
|
private void HandleOrientationCorrect()
|
|
{
|
|
if (promptInstance != null)
|
|
{
|
|
Destroy(promptInstance);
|
|
promptInstance = null;
|
|
}
|
|
if (orientationCheckCoroutine != null)
|
|
{
|
|
StopCoroutine(orientationCheckCoroutine);
|
|
orientationCheckCoroutine = null;
|
|
}
|
|
|
|
InputManager.Instance.SetInputMode(InputMode.Game);
|
|
}
|
|
|
|
private void CleanupPromptAndCoroutine()
|
|
{
|
|
if (promptInstance != null)
|
|
{
|
|
Destroy(promptInstance);
|
|
promptInstance = null;
|
|
}
|
|
if (orientationCheckCoroutine != null)
|
|
{
|
|
StopCoroutine(orientationCheckCoroutine);
|
|
orientationCheckCoroutine = null;
|
|
}
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
OnOrientationCorrect -= HandleOrientationCorrect;
|
|
}
|
|
|
|
void OnApplicationQuit()
|
|
{
|
|
_isQuitting = true;
|
|
}
|
|
}
|
|
}
|