using System; using System.Collections.Generic; using UnityEngine; namespace DuloGames.UI { public class UISceneRegistry { private static UISceneRegistry m_Instance; protected UISceneRegistry() { this.m_Scenes = new List(); } public static UISceneRegistry instance { get { if (m_Instance == null) m_Instance = new UISceneRegistry(); return m_Instance; } } private List m_Scenes; private UIScene m_LastScene; /// /// Get all the registered scenes. /// public UIScene[] scenes { get { return this.m_Scenes.ToArray(); } } /// /// Get the last active scene before the current one. /// public UIScene lastScene { get { return this.m_LastScene; } } /// /// Register a scene. /// /// public void RegisterScene(UIScene scene) { // Make sure we have the list set if (this.m_Scenes == null) { this.m_Scenes = new List(); } // Check if already registered if (this.m_Scenes.Contains(scene)) { Debug.LogWarning("Trying to register a UIScene multiple times."); return; } // Store in the list this.m_Scenes.Add(scene); } /// /// Unregister a scene. /// /// public void UnregisterScene(UIScene scene) { if (this.m_Scenes != null) { this.m_Scenes.Remove(scene); } } /// /// Get all the active scenes. /// /// public UIScene[] GetActiveScenes() { List activeScenes = this.m_Scenes.FindAll(x => x.isActivated == true); return activeScenes.ToArray(); } /// /// Get the scene with the specified id. Returns null if not found. /// /// /// public UIScene GetScene(int id) { if (this.m_Scenes == null || this.m_Scenes.Count == 0) { return null; } return this.m_Scenes.Find(x => x.id == id); } /// /// Get the next available scene id; /// /// public int GetAvailableSceneId() { if (this.m_Scenes.Count == 0) { return 0; } int id = 0; foreach (UIScene scene in this.m_Scenes) { if (scene.id > id) { id = scene.id; } } return id + 1; } /// /// Transitions out of the active scene and in to the new one. /// /// public void TransitionToScene(UIScene scene) { // Transition out of the current scenes UIScene[] activeScenes = this.GetActiveScenes(); foreach (UIScene activeScene in activeScenes) { // Transition the scene out activeScene.TransitionOut(); // Set as last scene this.m_LastScene = activeScene; } // Transition in the new scene scene.TransitionIn(); } } }