/// <summary> /// Gets the last set value for the specified type, but also tries to find one through <see cref="Resources.FindObjectsOfTypeAll"/> if none is found. /// </summary> /// <param name="type">The type of the settings.</param> /// <returns>The settings if set and still valid or if a new one could be found.</returns> protected static ScriptableSettings GetOrFind(Type type) { Debug.Assert(typeof(ScriptableSettings).IsAssignableFrom(type)); if (Values.TryGetValue(type, out ScriptableSettings value) && value.IsValid()) { return(value); } Object[] rawValues = Resources.FindObjectsOfTypeAll(type); if (rawValues.Length == 0) { return(null); } if (rawValues.Length > 1) { Debug.LogWarning($"It was expected a single loaded object of type {type}, but it was found {rawValues.Length}!"); } ScriptableSettings result = (ScriptableSettings)rawValues[0]; Values[type] = result; return(result); }
/// <summary> /// Sets the value for the specified type. /// </summary> /// <param name="type">The type of the settings.</param> /// <param name="value">The new value for the specified type.</param> /// <param name="forceSet">If false, it will not change the value if previously set.</param> protected static void Set(Type type, ScriptableSettings value, bool forceSet) { Debug.Assert(typeof(ScriptableSettings).IsAssignableFrom(type)); value = value.GetValid(); if (TryGet(type, out ScriptableSettings currentValue) && value != currentValue) { if (forceSet) { Debug.LogWarning($"Overriding {type} in {nameof(ScriptableSettings)} from \"{currentValue}\"!", currentValue); Debug.LogWarning($"Overriding {type} in {nameof(ScriptableSettings)} to \"{value}\"!", value); } else { Debug.LogWarning($"{type} in {nameof(ScriptableSettings)} is already set to \"{currentValue}\"!", currentValue); Debug.LogWarning($"{type} in {nameof(ScriptableSettings)} can't be overriden to \"{value}\".", value); return; } } if (value != null) { Values[type] = value; } else { Values.Remove(type); } }
/// <summary> /// Tries to get the last set value for the specified type, but also tries to find one through <see cref="Resources.FindObjectsOfTypeAll"/> if none is found. /// </summary> /// <param name="type">The type of the settings.</param> /// <param name="result">The settings if set and still valid or if a new one could be found.</param> /// <returns>The settings if set and still valid or if a new one could be found.</returns> protected static bool TryGetOrFind(Type type, out ScriptableSettings result) { Debug.Assert(typeof(ScriptableSettings).IsAssignableFrom(type)); result = GetOrFind(type); return(result != null); }