MIF_E31221357/Assets/Scripts/SceneNavigationManager.cs

76 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class SceneNavigationManager : MonoBehaviour
{
private static SceneNavigationManager instance;
private static List<string> sceneHistory = new List<string>();
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
Debug.Log("SceneNavigationManager dibuat");
}
}
public void LoadScene(string sceneName)
{
string currentScene = SceneManager.GetActiveScene().name;
if (sceneName == currentScene)
{
Debug.LogWarning("Scene yang dipilih sama dengan scene saat ini. Tidak berpindah.");
return;
}
// Simpan scene saat ini sebelum pindah ke scene baru
if (sceneHistory.Count == 0 || sceneHistory[sceneHistory.Count - 1] != currentScene)
{
sceneHistory.Add(currentScene);
}
// Gunakan LoadSceneAsync untuk perpindahan lebih lancar
SceneManager.LoadSceneAsync(sceneName).completed += (operation) =>
{
Debug.Log("Pindah ke scene: " + sceneName);
EnsureEventSystemExists();
};
}
public void GoBack()
{
if (sceneHistory.Count > 0)
{
string previousScene = sceneHistory[sceneHistory.Count - 1];
sceneHistory.RemoveAt(sceneHistory.Count - 1);
SceneManager.LoadSceneAsync(previousScene).completed += (operation) =>
{
Debug.Log("Kembali ke scene: " + previousScene);
EnsureEventSystemExists();
};
}
else
{
Debug.LogWarning("Tidak ada scene sebelumnya!");
}
}
// Pastikan EventSystem tetap ada
private void EnsureEventSystemExists()
{
if (FindFirstObjectByType<UnityEngine.EventSystems.EventSystem>() == null)
{
GameObject eventSystem = new GameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
DontDestroyOnLoad(eventSystem);
Debug.Log("EventSystem baru dibuat!");
}
}
}