Preliminary screen rotation work

This commit is contained in:
Michal Pikulski
2025-09-08 14:25:50 +02:00
parent 32888a6843
commit a1b1d7475f
15 changed files with 621 additions and 112 deletions

View File

@@ -201,6 +201,15 @@ public class SceneManagerService : MonoBehaviour
// Switches from current gameplay scene to a new one
public async Task SwitchSceneAsync(string newSceneName, IProgress<float> progress = null)
{
// Remove all AstarPath (A* Pathfinder) singletons before loading the new scene
var astarPaths = FindObjectsByType<AstarPath>(FindObjectsSortMode.None);
foreach (var astar in astarPaths)
{
if (Application.isPlaying)
Destroy(astar.gameObject);
else
DestroyImmediate(astar.gameObject);
}
// Load new scene
await LoadSceneAsync(newSceneName, progress);
// Unload previous scene (if not same)

View File

@@ -46,8 +46,23 @@ namespace Utility
void Start()
{
string sceneName = SceneManager.GetActiveScene().name;
requiredOrientation = orientationConfig != null ? orientationConfig.GetRequirementForScene(sceneName) : ScreenOrientationRequirement.Portrait;
// 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)
{
@@ -56,14 +71,20 @@ namespace Utility
}
}
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";
}
private bool IsOrientationCorrect()
{
switch (requiredOrientation)
{
case ScreenOrientationRequirement.Portrait:
return Screen.height >= Screen.width;
return Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown;
case ScreenOrientationRequirement.Landscape:
return Screen.width > Screen.height;
return Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight;
default:
return true;
}
@@ -101,8 +122,23 @@ namespace Utility
}
}
private void CleanupPromptAndCoroutine()
{
if (promptInstance != null)
{
Destroy(promptInstance);
promptInstance = null;
}
if (orientationCheckCoroutine != null)
{
StopCoroutine(orientationCheckCoroutine);
orientationCheckCoroutine = null;
}
}
void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
OnOrientationCorrect -= HandleOrientationCorrect;
}