Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using Multiton.Containers;
- public static class Multiton<T> where T : SingletonOfType
- {
- /// <summary>
- /// Checks within the static dictionary if it's possible to add a Singleton that exists of a certain type
- /// </summary>
- /// <param name="_singletonType"></param>
- /// <returns></returns>
- public static bool TryAddSingleton(T _singletonType)
- {
- if(!MultitonContainer.SingletonCollection.ContainsKey(_singletonType))
- {
- MultitonContainer.SingletonCollection.Add(_singletonType, _singletonType);
- Debug.LogFormat($"Succesfully added: { _singletonType.GetType() } to the static Multiton dictionary");
- return true;
- }
- return false;
- }
- /// <summary>
- /// Atttempts to return a co-responding type of singleton
- /// </summary>
- /// <returns></returns>
- public static T TryGetSingletonOfType()
- {
- foreach (object type in MultitonContainer.SingletonCollection.Values)
- {
- if (type.GetType().Equals(typeof(T)))
- {
- return (T)type;
- }
- }
- return default;
- }
- /// <summary>
- /// Attempts to remove a possibly existing Singleton based on the given parameter
- /// </summary>
- /// <param name="_singletonType"></param>
- /// <returns></returns>
- public static bool TryRemoveSingletonOfType(T _singletonType)
- {
- if(MultitonContainer.SingletonCollection.ContainsKey(_singletonType))
- {
- MultitonContainer.SingletonCollection.Remove(_singletonType);
- return true;
- }
- return false;
- }
- /// <summary>
- /// Clears the Multiton out of all the contained Singletons
- /// </summary>
- public static void ClearAllSingletons()
- {
- MultitonContainer.SingletonCollection.Clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement