-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumGenerator.cs
More file actions
179 lines (140 loc) · 5.95 KB
/
EnumGenerator.cs
File metadata and controls
179 lines (140 loc) · 5.95 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace CustomResourceManagement
{
#if UNITY_EDITOR
public class EnumGenerator : EditorWindow
{
// Selected scrip that saves generated enums.
private string scriptPath;
private string scriptName;
private Stack<StringBuilder> previousSources = new Stack<StringBuilder>();
string editorLogMessage = string.Empty;
private StringBuilder GenerateNewScript(bool saveThisScript = true)
{
var sourceCode = new StringBuilder("// This script's automatically generated by EnumGenerator.cs\n");
sourceCode.AppendLine($"namespace {typeof(EnumGenerator).Namespace}\n{{");
var folderInfo = new DirectoryInfo($"{Application.dataPath}/Resources");
ProcessFolder(folderInfo, sourceCode);
sourceCode.AppendLine("}");
return GenerateNewScript(sourceCode, saveThisScript);
}
private void ProcessFolder(DirectoryInfo directoryInfo, StringBuilder sourceCode, int callCount = 0)
{
var subFolders = directoryInfo.GetDirectories();
var tabSpace = new string('\t', callCount);
if (subFolders.Length > 0)
{
if (callCount > 0)
{
var structName = directoryInfo.Name;
sourceCode.AppendLine($"{tabSpace}public struct {structName}{{");
}
foreach (var subFolder in subFolders)
ProcessFolder(subFolder, sourceCode, callCount + 1);
if (callCount > 0)
sourceCode.AppendLine($"{tabSpace}}}");
}
else
{
var enumName = directoryInfo.Name;
sourceCode.AppendLine($"{tabSpace}public enum {enumName}{{");
var files = directoryInfo.GetFiles("*", SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
if (file.Extension != ".meta")
{
var enumMember = file.Name.Replace(file.Extension, string.Empty);
sourceCode.AppendLine($"{tabSpace}\t{enumMember},");
}
}
sourceCode.AppendLine($"{tabSpace}}}");
}
}
private StringBuilder GenerateNewScript(StringBuilder sourceCode, bool saveThisScript = true)
{
if (File.Exists(scriptPath))
File.WriteAllText(scriptPath, string.Empty); // This is how you should clear contents of a text file. (better than File.Delete();)
else
{
editorLogMessage = " <color=red>File Missing!</color>";
return null;
}
File.AppendAllText(scriptPath, sourceCode + "\n");
if (!saveThisScript)
{
editorLogMessage = " <color=cyan>Reverted Successfully</color>";
return null;
}
AssetDatabase.Refresh();
return sourceCode;
}
[MenuItem("Window/Custom Asset Manager")]
public static void ShowWindow()
{
GetWindow(typeof(EnumGenerator));
}
void OnGUI()
{
if (!string.IsNullOrEmpty(scriptPath))
{
GUIStyle labelStyle = new GUIStyle(GUI.skin.label);
labelStyle.fontStyle = FontStyle.Bold;
labelStyle.normal.textColor = Color.green;
GUILayout.Label($"Selected File: {scriptName}.cs", labelStyle);
}
else
{
GUILayout.Label("Drag & Drop C# script here", EditorStyles.boldLabel);
}
Rect labelRect = GUILayoutUtility.GetLastRect();
HandleDragAndDrop(labelRect);
GUIStyle logStyle = new GUIStyle();
logStyle.fontStyle = FontStyle.Italic;
GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
buttonStyle.fontStyle = FontStyle.Bold;
GUILayout.Label(editorLogMessage, logStyle);
if (GUILayout.Button("Save", buttonStyle))
{
editorLogMessage = " <color=yellow>Saving...</color>";
var newScript = GenerateNewScript();
if (newScript != null)
{
editorLogMessage = " <color=cyan>Saved Successfully</color>";
previousSources.Push(newScript);
}
}
if (GUILayout.Button("Revert", buttonStyle))
{
if (previousSources.Count == 0)
{
editorLogMessage = " <color=orange>Can't revert - no longer have saved data";
return;
}
GenerateNewScript(previousSources.Pop(), saveThisScript: false);
}
}
private void HandleDragAndDrop(Rect dropArea)
{
Event currentEvent = Event.current;
if (currentEvent.type != EventType.DragUpdated && currentEvent.type != EventType.DragPerform) return;
if (!dropArea.Contains(currentEvent.mousePosition)) return;
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (currentEvent.type != EventType.DragPerform) return;
DragAndDrop.AcceptDrag();
currentEvent.Use();
foreach (var draggedObject in DragAndDrop.objectReferences)
{
if (draggedObject is MonoScript script)
{
scriptPath = AssetDatabase.GetAssetPath(script);
scriptName = script.name;
}
}
}
}
#endif
}