Add main menu scene, setup framework for orientation switching
This commit is contained in:
114
Assets/Scripts/Utility/SceneOrientationEnforcer.cs
Normal file
114
Assets/Scripts/Utility/SceneOrientationEnforcer.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System;
|
||||
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()
|
||||
{
|
||||
string sceneName = SceneManager.GetActiveScene().name;
|
||||
requiredOrientation = orientationConfig != null ? orientationConfig.GetRequirementForScene(sceneName) : ScreenOrientationRequirement.Portrait;
|
||||
orientationCorrect = IsOrientationCorrect();
|
||||
if (!orientationCorrect)
|
||||
{
|
||||
ShowPrompt();
|
||||
orientationCheckCoroutine = StartCoroutine(OrientationCheckRoutine());
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsOrientationCorrect()
|
||||
{
|
||||
switch (requiredOrientation)
|
||||
{
|
||||
case ScreenOrientationRequirement.Portrait:
|
||||
return Screen.height >= Screen.width;
|
||||
case ScreenOrientationRequirement.Landscape:
|
||||
return Screen.width > Screen.height;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleOrientationCorrect()
|
||||
{
|
||||
if (promptInstance != null)
|
||||
{
|
||||
Destroy(promptInstance);
|
||||
promptInstance = null;
|
||||
}
|
||||
if (orientationCheckCoroutine != null)
|
||||
{
|
||||
StopCoroutine(orientationCheckCoroutine);
|
||||
orientationCheckCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
OnOrientationCorrect -= HandleOrientationCorrect;
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
_isQuitting = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Utility/SceneOrientationEnforcer.cs.meta
Normal file
3
Assets/Scripts/Utility/SceneOrientationEnforcer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5be782e6ff7641519b9bad2e7552d4e8
|
||||
timeCreated: 1757316712
|
||||
Reference in New Issue
Block a user