/// <summary> /// Get a reference to the registered type requested. Returns first found. /// Only use with non-repeatable types /// </summary> /// <typeparam name="T">The MonoBehaviour type to get</typeparam> /// <param name="title">The key to search for among the dictionary</param> /// <returns>A reference to the requested type, if the type isn't registered returns null</returns> public T GetType <T>(GameTypeTitle title) where T : MonoBehaviour { if (this.registeredTypes.ContainsKey(title)) { this.registeredTypes.TryGetValue(title, out List <MonoBehaviour> value); return((T)value[0]); } return(null); }
/// <summary> /// Register the given type so that only one of the type with the associated key exists at run time. /// Call function in Awake() /// </summary> /// <param name="mb">The type to register</param> /// <param name="title">The key to associate it with</param> public void RegisterType(MonoBehaviour mb, GameTypeTitle title, bool repeatable) { TypeInfo type = mb.GetType().GetTypeInfo(); if (this.registeredTypes.ContainsKey(title) && !repeatable) { Debug.LogWarning("[GameController] attempted to register duplicate keys for type: " + title); } else if (!this.registeredTypes.ContainsKey(title)) { List <MonoBehaviour> lmb = new List <MonoBehaviour>(); lmb.Add(mb); this.registeredTypes.Add(title, lmb); } else { this.registeredTypes.TryGetValue(title, out List <MonoBehaviour> list); list.Add(mb); Debug.Log("[GameController] Type registered: " + mb); } }
/// <summary> /// Checks if the type is registered to the GameController. /// </summary> /// <returns>If type was registered</returns> public bool HasType(GameTypeTitle title) { return(registeredTypes.ContainsKey(title)); }
/// <summary> /// Removes registered type with the given key /// </summary> /// <param name="title">Key of the registered type to remove</param> public void UnregisterType(GameTypeTitle title) { this.registeredTypes.Remove(title); }