forked from RonenNess/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityConstantsGenerator.cs
More file actions
148 lines (132 loc) · 6.62 KB
/
UnityConstantsGenerator.cs
File metadata and controls
148 lines (132 loc) · 6.62 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
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
namespace UnityToolbag
{
public static class UnityConstantsGenerator
{
[MenuItem("Edit/Generate UnityConstants.cs")]
public static void Generate()
{
// Try to find an existing file in the project called "UnityConstants.cs"
string filePath = string.Empty;
foreach (var file in Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories)) {
if (Path.GetFileNameWithoutExtension(file) == "UnityConstants") {
filePath = file;
break;
}
}
// If no such file exists already, use the save panel to get a folder in which the file will be placed.
if (string.IsNullOrEmpty(filePath)) {
string directory = EditorUtility.OpenFolderPanel("Choose location for UnityConstants.cs", Application.dataPath, "");
// Canceled choose? Do nothing.
if (string.IsNullOrEmpty(directory)) {
return;
}
filePath = Path.Combine(directory, "UnityConstants.cs");
}
// Write out our file
using (var writer = new StreamWriter(filePath)) {
writer.WriteLine("// This file is auto-generated. Modifications are not saved.");
writer.WriteLine();
writer.WriteLine("namespace UnityConstants");
writer.WriteLine("{");
// Write out the tags
writer.WriteLine(" public static class Tags");
writer.WriteLine(" {");
foreach (var tag in UnityEditorInternal.InternalEditorUtility.tags) {
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// Name of tag '{0}'.", tag);
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public const string {0} = \"{1}\";", MakeSafeForCode(tag), tag);
}
writer.WriteLine(" }");
writer.WriteLine();
// Write out sorting layers
writer.WriteLine(" public static class SortingLayers");
writer.WriteLine(" {");
foreach (var layer in SortingLayer.layers) {
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// ID of sorting layer '{0}'.", layer.name);
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(layer.name), layer.id);
}
writer.WriteLine(" }");
writer.WriteLine();
// Write out layers
writer.WriteLine(" public static class Layers");
writer.WriteLine(" {");
for (int i = 0; i < 32; i++) {
string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i);
if (!string.IsNullOrEmpty(layer)) {
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// Index of layer '{0}'.", layer);
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(layer), i);
}
}
writer.WriteLine();
for (int i = 0; i < 32; i++) {
string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i);
if (!string.IsNullOrEmpty(layer)) {
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// Bitmask of layer '{0}'.", layer);
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public const int {0}Mask = 1 << {1};", MakeSafeForCode(layer), i);
}
}
writer.WriteLine(" }");
writer.WriteLine();
// Write out scenes
writer.WriteLine(" public static class Scenes");
writer.WriteLine(" {");
int sceneIndex = 0;
foreach (var scene in EditorBuildSettings.scenes) {
if (!scene.enabled) {
continue;
}
var sceneName = Path.GetFileNameWithoutExtension(scene.path);
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// ID of scene '{0}'.", sceneName);
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(sceneName), sceneIndex);
sceneIndex++;
}
writer.WriteLine(" }");
writer.WriteLine();
// Write out Input axes
writer.WriteLine(" public static class Axes");
writer.WriteLine(" {");
var axes = new HashSet<string>();
var inputManagerProp = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
foreach (SerializedProperty axe in inputManagerProp.FindProperty("m_Axes")) {
var name = axe.FindPropertyRelative("m_Name").stringValue;
var variableName = MakeSafeForCode(name);
if (!axes.Contains(variableName)) {
writer.WriteLine(" /// <summary>");
writer.WriteLine(" /// Input axis '{0}'.", name);
writer.WriteLine(" /// </summary>");
writer.WriteLine(" public const string {0} = \"{1}\";", variableName, name);
axes.Add(variableName);
}
}
writer.WriteLine(" }");
// End of namespace UnityConstants
writer.WriteLine("}");
writer.WriteLine();
}
// Refresh
AssetDatabase.Refresh();
}
private static string MakeSafeForCode(string str)
{
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_", RegexOptions.Compiled);
if (char.IsDigit(str[0])) {
str = "_" + str;
}
return str;
}
}
}