Skip to content

Instantly share code, notes, and snippets.

@douduck08
Last active March 24, 2021 15:32
Show Gist options
  • Save douduck08/9fec275e9539d9be970ef8989ac62df6 to your computer and use it in GitHub Desktop.
Save douduck08/9fec275e9539d9be970ef8989ac62df6 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public interface IService {}
public abstract class InitialzeArgs {}
public interface IInitializable {
void Initialize (InitialzeArgs args);
}
public interface IUpdatable {
void OnUpdate ();
}
public interface IFixedUpdatable {
void OnFixedUpdate ();
}
public interface ILateUpdatable {
void OnLateUpdate ();
}
public sealed class ServiceLocator : SingletonMono<ServiceLocator> {
private Dictionary<Type, IService> m_serviceDictionary = new Dictionary<Type, IService> ();
private List<IUpdatable> m_updatableList = new List<IUpdatable> ();
private List<IFixedUpdatable> m_FixedUpdatableList = new List<IFixedUpdatable> ();
private List<ILateUpdatable> m_lateUpdatableList = new List<ILateUpdatable> ();
public static void Register<T> (T service, InitialzeArgs initialzeArgs = null) where T : IService {
Type serviceType_ = typeof(T);
if (Instance.m_serviceDictionary.ContainsKey (typeof (T))) {
throw new InvalidOperationException (string.Format("[ServiceLocator] There was a service <{0}>", serviceType_.Name));
}
Instance.m_serviceDictionary.Add (serviceType_, service as IService);
IInitializable initializable_ = service as IInitializable;
if (initializable_ != null) {
initializable_.Initialize (initialzeArgs);
}
IUpdatable updatable_ = service as IUpdatable;
if (updatable_ != null) {
Instance.m_updatableList.Add (updatable_);
}
IFixedUpdatable fixedUpdatable_ = service as IFixedUpdatable;
if (fixedUpdatable_ != null) {
Instance.m_FixedUpdatableList.Add (fixedUpdatable_);
}
ILateUpdatable lateUpdatable_ = service as ILateUpdatable;
if (lateUpdatable_ != null) {
Instance.m_lateUpdatableList.Add (lateUpdatable_);
}
}
public static void Register<T> (InitialzeArgs initialzeArgs = null) where T : IService, new () {
Type serviceType_ = typeof(T);
if (Instance.m_serviceDictionary.ContainsKey (typeof (T))) {
throw new InvalidOperationException (string.Format("[ServiceLocator] There was a service <{0}>", serviceType_.Name));
}
if (serviceType_.IsSubclassOf(typeof(MonoBehaviour))) {
throw new ArgumentException (string.Format("[ServiceLocator] Service <{0}> is a MonoBehaviour, use 'RegisterMono ()' instead", serviceType_.Name));
}
ServiceLocator.Register<T> (new T(), initialzeArgs);
}
public static void RegisterMono<T> (InitialzeArgs initialzeArgs = null) where T : MonoBehaviour, IService {
Type serviceType_ = typeof(T);
if (Instance.m_serviceDictionary.ContainsKey (typeof (T))) {
throw new InvalidOperationException (string.Format("[ServiceLocator] There was a service <{0}>", serviceType_.Name));
}
T service_ = Instance.gameObject.AddComponent<T>();
ServiceLocator.Register<T> (service_, initialzeArgs);
}
public static T GetService<T> () where T : class, IService {
if (Instance.m_serviceDictionary.ContainsKey (typeof (T))) {
return Instance.m_serviceDictionary[typeof (T)] as T;
} else {
return null;
}
}
void Update () {
for (int i = m_updatableList.Count - 1; i >= 0; i--) {
m_updatableList[i].OnUpdate ();
}
}
void FixedUpdate () {
for (int i = m_FixedUpdatableList.Count - 1; i >= 0; i--) {
m_FixedUpdatableList[i].OnFixedUpdate ();
}
}
void LateUpdate () {
for (int i = m_lateUpdatableList.Count - 1; i >= 0; i--) {
m_lateUpdatableList[i].OnLateUpdate ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment