using UnityEngine; namespace Unity.XR.CoreUtils { /// /// A helper class for accessing settings stored in instances. /// /// A class derived from . public abstract class ScriptableSettings : ScriptableSettingsBase where T : ScriptableObject { const string k_CustomSavePathFormat = "{0}Resources/{1}.asset"; const string k_SavePathFormat = "{0}Resources/ScriptableSettings/{1}.asset"; const string k_LoadPathFormat = "ScriptableSettings/{0}"; /// /// Retrieves a reference to the given settings class. Loads and initializes the class once, and caches the reference for all future access. /// /// A settings class derived from . public static T Instance { get { if (BaseInstance == null) CreateAndLoad(); return BaseInstance; } } internal static T CreateAndLoad() { System.Diagnostics.Debug.Assert(BaseInstance == null); // Try to load the singleton var path = HasCustomPath ? GetFilePath() : string.Format(k_LoadPathFormat, GetFilePath()); BaseInstance = Resources.Load(path) as T; // Create it if it doesn't exist if (BaseInstance == null) { BaseInstance = CreateInstance(); // And save it back out if appropriate Save(HasCustomPath ? k_CustomSavePathFormat : k_SavePathFormat); } System.Diagnostics.Debug.Assert(BaseInstance != null); return BaseInstance; } } }