You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/3rd/D2D_Scripts/Utilities/SingletonData.cs

50 lines
1.5 KiB
C#

using UnityEditor;
using UnityEngine;
namespace D2D.Utilities
{
/// <summary>
/// If it is necessary to have just one SO of this type
/// and u wont get quick access to this SO from code => derive from this class.
/// </summary>
/// <typeparam name="T">Any type of SO</typeparam>
public abstract class SingletonData<T> : ScriptableObject
where T : ScriptableObject
{
private const string PathToResourcesFolder = "Assets/Resources";
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
HashInstance();
return _instance;
}
}
private static void HashInstance()
{
// Find the SO asset
_instance = Resources.Load<T>(typeof(T).Name);
#if UNITY_EDITOR
if (_instance == null)
{
// Create SO
_instance = CreateInstance<T>();
// Create Resources folder if not exists
if (!AssetDatabase.IsValidFolder(PathToResourcesFolder))
AssetDatabase.CreateFolder("Assets", "Resources");
// Create SO asset automatically if not exists
AssetDatabase.CreateAsset(_instance, $"{PathToResourcesFolder}/{typeof(T).Name}.asset");
}
#endif
}
}
}