-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetManager.cs
126 lines (102 loc) · 4.58 KB
/
AssetManager.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public class AssetManager : MonoBehaviour
{
public static GameObject GetPrefab(string folderName, string prefabName)
{
var prefab = Resources.Load<GameObject>($"Prefabs/{folderName}/{prefabName}");
if (prefab is null)
{
Debug.LogWarning($"Invalid name or path: Prefabs/{folderName}/{prefabName}");
return null;
}
return prefab;
}
public static GameObject GetPrefab<T>(T prefabType) where T : Enum
{
ReadOnlySpan<char> span = typeof(T).ToString().AsSpan();
ReadOnlySpan<char> result = span.Slice(nameof(Prefab).Length + 1);
return GetPrefab(result.ToString(), prefabType.ToString());
}
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(AssetManager))]
public class PrefabScriptEditor : Editor
{
// Rename these variables depending on your directory structure.
private static readonly string FolderName = "Prefabs"; // Name of the folder where prefab files are located in the project.
private static readonly string ScriptPath = "Scripts/Prefab.cs" // This is where the script's going to be generated in the project.
private readonly string Path_Resources = $"{Application.dataPath}/Resources/{FolderName}";
private readonly string Path_PrefabScript = $"{Application.dataPath}/{ScriptPath}}";
private Stack<StringBuilder> previousSources = new Stack<StringBuilder>();
private StringBuilder GenerateNewScript(bool saveThisScript = true)
{
var sourceCode = new StringBuilder("// This script's automatically generated by AssetManager.cs");
sourceCode.AppendLine("\npublic struct Prefab {");
var enumInfo = new DirectoryInfo(Path_Resources);
var enums = enumInfo.GetDirectories(); // Use GetDirectories() to get folders' name cuz they don't have file extenstion.
for (int i = 0; i < enums.Length; i++)
{
sourceCode.AppendLine($"\tpublic enum {enums[i].Name} {{"); // Must use item.Name, or you'll get the full name of it including the path.
var constantInfo = new DirectoryInfo($"{Path_Resources}/{enums[i].Name}");
var constants = constantInfo.GetFiles("*.prefab", SearchOption.AllDirectories); // Use GetFiles() to get files' name.
// Avoid loading .meta files either by using "*.extensionName".
for (int j = 0; j < constants.Length; j++)
{
sourceCode.AppendLine($"\t\t{constants[j].Name.Replace(".prefab","")},"); // Don't forget to remove file extension.
}
sourceCode.AppendLine("\t}");
}
sourceCode.AppendLine("}");
return GenerateNewScript(sourceCode, saveThisScript);
}
private StringBuilder GenerateNewScript(StringBuilder sourceCode, bool saveThisScript = true)
{
if (File.Exists(Path_PrefabScript))
File.WriteAllText(Path_PrefabScript, string.Empty); // This is how you should clear contents of a text file. (better than File.Delete();)
else
{
Debug.LogWarning($"<color=red>File Missing!\nPrefab.cs is not in the folder: {Path_PrefabScript}</color>");
return null;
}
File.AppendAllText(Path_PrefabScript, sourceCode + "\n");
if (!saveThisScript)
{
Debug.Log("<color=cyan>Reverted Completely</color>");
return null;
}
Debug.Log("<color=cyan>Save Completed</color>");
return sourceCode;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Save Prefabs"))
{
Debug.Log("<color=yellow>Saving..</color>");
var newScript = GenerateNewScript();
if (newScript is null) return;
previousSources.Push(newScript);
}
if (GUILayout.Button("Revert Changes"))
{
if (previousSources.Count == 0)
{
Debug.LogWarning("Can't revert - no longer have saved data");
return;
}
GenerateNewScript(previousSources.Pop(), saveThisScript: false);
}
GUILayout.EndHorizontal();
}
}
#endif