using UnityEngine;
using System.Collections.Generic;
namespace DuloGames.UI
{
[DisallowMultipleComponent, ExecuteInEditMode, AddComponentMenu("UI/UI Scene/Manager")]
public class UISceneManager : MonoBehaviour
{
private static UISceneManager m_Instance;
///
/// Get the scene manager if any exists.
///
public static UISceneManager instance
{
get { return m_Instance; }
}
private List m_Scenes;
///
/// Get all the registered scenes.
///
public UIScene[] scenes
{
get { return this.m_Scenes.ToArray(); }
}
protected void Awake()
{
// Check if we already have a scene manager
if (m_Instance != null)
{
if (Application.isPlaying)
{
Destroy(this);
}
else
{
DestroyImmediate(this);
}
Debug.LogWarning("Multiple UISceneManagers are not allowed, destroying.");
return;
}
// Save reference in the static variable
m_Instance = this;
// Prepare the list
if (this.m_Scenes == null)
{
this.m_Scenes = new List();
}
// Prevent destruction on scene load
if (Application.isPlaying)
{
DontDestroyOnLoad(this.gameObject);
}
}
protected void OnDestroy()
{
// Remove reference
m_Instance = null;
}
///
/// 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)
{
UIScene.Transition transition = scene.transition;
float transitionDuration = scene.transitionDuration;
Tweens.TweenEasing transitionEasing = scene.transitionEasing;
// Transition out of the current scenes
UIScene[] activeScenes = this.GetActiveScenes();
foreach (UIScene activeScene in activeScenes)
{
// Transition the scene out
activeScene.TransitionOut(transition, transitionDuration, transitionEasing);
}
// Transition in the new scene
scene.TransitionIn(transition, transitionDuration, transitionEasing);
}
}
}