-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSingleEditorDataObject.cs
More file actions
90 lines (75 loc) · 3.22 KB
/
SingleEditorDataObject.cs
File metadata and controls
90 lines (75 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Created by Alex Meesters
// www.low-scope.com, www.alexmeesters.nl
// Licenced under the MIT Licence. https://en.wikipedia.org/wiki/MIT_License
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Lowscope
{
/// <summary>
/// Generates an .asset file specifically for a class (T) upon access. Gets placed in "Editor Default Resources"
/// This is purely if you want a class to act as a singleton for specific data.
/// Useful if you require asset references from the editor, such as prefabs.
///
/// Simply create a ScriptableObject that derives from EditorDataObject<ScriptableObjectTypeName>.
/// You can then reference assets and create static methods/fields to access them.
/// For instance: public static string MySceneName => Instance.mySceneName;
/// </summary>
/// <typeparam name="T">Type of the ScriptableObject </typeparam>
[Serializable]
abstract public class SingleEditorDataObject<T> : ScriptableObject where T : ScriptableObject
{
public static T Instance => LoadInstance();
private static T instance = null;
private static T LoadInstance()
{
if (instance != null)
return instance;
// Get instance of the attribute.
EditorDataObjectName getAttribute = (EditorDataObjectName)Attribute.GetCustomAttribute
(typeof(T), typeof(EditorDataObjectName));
string typeName = (getAttribute != null)? getAttribute.GetName() : typeof(T).ToString();
string editorResourcesPath = $"{Application.dataPath}/Editor Default Resources";
string classPath = $"{editorResourcesPath}/{typeName}.asset";
// Create Editor Default Resources folder if it does not exist.
if (!Directory.Exists(editorResourcesPath))
{
Directory.CreateDirectory(editorResourcesPath);
}
// Create scriptable object .asset file if it does not exist.
if (!File.Exists(classPath))
{
var createInstance = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(createInstance, $"Assets/Editor Default Resources/{typeName}.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
instance = createInstance;
(instance as SingleEditorDataObject<T>).OnCreated();
return createInstance;
}
// Try to load the file if it does exist.
T loadInstance = AssetDatabase.LoadAssetAtPath<T>($"Assets/Editor Default Resources/{typeName}.asset");
if (loadInstance == null)
{
loadInstance = EditorGUIUtility.Load($"{typeName}") as T;
}
instance = (T)loadInstance;
return instance;
}
protected virtual void OnCreated() { }
}
[System.AttributeUsage(System.AttributeTargets.Class)]
public class EditorDataObjectName : System.Attribute
{
private string name;
public string GetName()
{
return name;
}
public EditorDataObjectName(string name)
{
this.name = name;
}
}
}