using System;
using UnityEngine;
using Framework.Support;
namespace Framework.Services {
/// Manages and provides access to a game object holding the services
public static class ServicesRoot {
/// Name of the game object that will carry the services
public const string ServicesGameObjectName = "Services";
/// Retrieves the game object holding the services
/// The game object responsible for holding the services
public static GameObject Get() {
if(GameObjectHelper.IsNullOrDestroyed(servicesGameObject)) {
servicesGameObject = GameObject.Find("/" + ServicesGameObjectName);
}
return servicesGameObject;
}
/// Retrieves or creates the game object holding the services
/// The game object responsible for holding the services
public static GameObject GetOrCreate() {
if(GameObjectHelper.IsNullOrDestroyed(servicesGameObject)) {
servicesGameObject = GameObject.Find("/" + ServicesGameObjectName);
// No service container found, create a new one
if(servicesGameObject == null) {
servicesGameObject = new GameObject(ServicesGameObjectName);
}
// For some reason, this DontDestroyOnLoad() only works in the editor, so to
// ensure the game object really stays around, we also use a special component.
GameObject.DontDestroyOnLoad(servicesGameObject);
servicesGameObject.AddComponent();
}
return servicesGameObject;
}
/// Destroys all services in the specified scope
public static void Kill(Scope scope) {
// It may be that a service container was carried over from the previous scene
// but it wasn't looked up yet.
if(GameObjectHelper.IsNullOrDestroyed(servicesGameObject)) {
servicesGameObject = GameObject.Find("/" + ServicesGameObjectName);
}
if(!GameObjectHelper.IsNullOrDestroyed(servicesGameObject)) {
GameObject.Destroy(servicesGameObject);
servicesGameObject = null;
}
}
/// Game object that is holding the services
private static GameObject servicesGameObject;
}
} // namespace Framework.Services