-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateScriptWindow.cs
More file actions
227 lines (174 loc) · 6.91 KB
/
Copy pathCreateScriptWindow.cs
File metadata and controls
227 lines (174 loc) · 6.91 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) Rotorz Limited. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root.
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
namespace ScriptTemplates {
public class CreateScriptWindow : EditorWindow {
[MenuItem("Window/Create Script from Template")]
private static void ShowWindow() {
GetWindow<CreateScriptWindow>("Create Script");
}
[SerializeField]
private string _scriptName = "";
[SerializeField]
private string _ns = "";
[SerializeField]
private bool _nsForSubFolders;
[NonSerialized]
private string[] _templateDescriptions;
[SerializeField]
private int _templateIndex;
[NonSerialized]
private ScriptTemplateGenerator _activeGenerator;
private void OnEnable() {
minSize = new Vector2(230, 100);
_nsForSubFolders = EditorPrefs.GetBool("ScriptTemplates.NamespaceToDirectory", true);
AutoFixActiveGenerator();
}
private Vector2 _scrollPosition;
private void OnGUI() {
EditorGUILayout.Space();
DrawTemplateSelector();
EditorGUILayout.Space();
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);
{
DrawStandardInputs();
EditorGUILayout.Space();
_activeGenerator.OnGUI();
GUILayout.FlexibleSpace();
}
EditorGUILayout.EndScrollView();
GUILayout.Box(GUIContent.none, GUILayout.Height(2), GUILayout.ExpandWidth(true));
_activeGenerator.OnStandardGUI();
DrawButtonStrip();
EditorGUILayout.Space();
}
private void DrawTemplateSelector() {
if (_templateDescriptions == null)
_templateDescriptions = ScriptGeneratorDescriptor.Descriptors
.Select(descriptor => descriptor.Attribute.Description)
.ToArray();
// Allow user to select template from popup.
EditorGUILayout.PrefixLabel("Template:");
EditorGUI.BeginChangeCheck();
_templateIndex = EditorGUILayout.Popup(_templateIndex, _templateDescriptions);
if (EditorGUI.EndChangeCheck())
_activeGenerator = null;
AutoFixActiveGenerator();
}
private void DrawStandardInputs() {
EditorGUILayout.PrefixLabel("Script Name:");
_scriptName = EditorGUILayout.TextField(_scriptName);
EditorGUILayout.PrefixLabel("Namespace:");
_ns = EditorGUILayout.TextField(_ns);
EditorGUI.BeginChangeCheck();
_nsForSubFolders = EditorGUILayout.ToggleLeft("Use namespace for sub-folders", _nsForSubFolders);
if (EditorGUI.EndChangeCheck())
EditorPrefs.SetBool("ScriptTemplates.NamespaceToDirectory", _nsForSubFolders);
}
private void DrawButtonStrip() {
GUILayout.Box(GUIContent.none, GUILayout.Height(2), GUILayout.ExpandWidth(true));
if (GUILayout.Button("Save at Default Path"))
DoSaveAtDefaultPath();
if (GUILayout.Button("Save As"))
DoSaveAs();
EditorGUILayout.Space();
if (GUILayout.Button("Copy to Clipboard"))
DoCopyToClipboard();
}
private void AutoFixActiveGenerator() {
if (_activeGenerator == null) {
_templateIndex = Mathf.Clamp(_templateIndex, 0, ScriptGeneratorDescriptor.Descriptors.Count);
_activeGenerator = ScriptGeneratorDescriptor.Descriptors[_templateIndex].CreateInstance();
}
}
private void ResetInputs() {
AutoFixActiveGenerator();
_scriptName = "";
_ns = "";
}
private string GetDefaultOutputPath() {
string assetFolder = Path.Combine("Assets", _activeGenerator.WillGenerateEditorScript ? "Editor/Scripts" : "Scripts");
// Use namespace for sub-folders?
if (_nsForSubFolders && !string.IsNullOrEmpty(_ns))
assetFolder = Path.Combine(assetFolder, _ns.Replace("_", "/"));
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), assetFolder);
// Ensure that this path actually exists.
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);
return outputPath;
}
private void DoSaveAtDefaultPath() {
GenerateScriptFromTemplate(Path.Combine(GetDefaultOutputPath(), _scriptName + ".cs"));
}
private void DoSaveAs() {
// Prompt user to specify path to save script.
string path = EditorUtility.SaveFilePanel("Save New Script", GetDefaultOutputPath(), _scriptName + ".cs", ".cs");
if (!string.IsNullOrEmpty(path))
GenerateScriptFromTemplate(path);
}
private static bool IsClassNameUnique(string fullName) {
if (string.IsNullOrEmpty(fullName))
throw new InvalidOperationException("An empty or null string was specified.");
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (var type in assembly.GetTypes())
if (type.FullName == fullName)
return false;
return true;
}
private void ValidateScriptInputs() {
// Ensure that valid script name was specified.
if (!Regex.IsMatch(_scriptName, @"^[A-za-z_][A-za-z_0-9]*$")) {
EditorUtility.DisplayDialog("Invalid Script Name", string.Format("'{0}' is not a valid type name.", _scriptName), "OK");
return;
}
// If a namespace was specified, ensure that it is valid!
if (!string.IsNullOrEmpty(_ns) && !Regex.IsMatch(_ns, @"^[A-za-z_][A-za-z_0-9]*(\.[A-za-z_][A-za-z_0-9]*)*$")) {
EditorUtility.DisplayDialog("Invalid Namespace", string.Format("'{0}' is not a valid namespace.", _ns), "OK");
return;
}
}
private void GenerateScriptFromTemplate(string path) {
// Ensure that input focus is removed from text field.
EditorGUIUtility.keyboardControl = 0;
EditorGUIUtility.editingTextField = false;
// Ensure that path ends with '.cs'.
if (!path.EndsWith(".cs")) {
EditorUtility.DisplayDialog("Invalid File Extension", "Could not save script because the wrong file extension was specified.\n\nPlease ensure to save as '.cs' file.", "OK");
return;
}
// Ensure that base directory actually exists.
if (!Directory.Exists(Path.GetDirectoryName(path))) {
EditorUtility.DisplayDialog("Invalid Path", "Could not save script because the specified directory does not exist.", "OK");
return;
}
ValidateScriptInputs();
string fullName = !string.IsNullOrEmpty(_ns)
? _ns + "." + _scriptName
: _scriptName;
// Warn user if their type name is not unique.
if (!IsClassNameUnique(fullName))
if (!EditorUtility.DisplayDialog("Warning: Type Already Exists!", string.Format("A type already exists with the name '{0}'.\n\nIf you proceed then you will get compilation errors in the console window.", fullName), "Proceed", "Cancel"))
return;
// Generate source code.
string sourceCode = _activeGenerator.GenerateScript(_scriptName, _ns);
// Write to file!
File.WriteAllText(path, sourceCode, Encoding.UTF8);
// Unity should now recompile its scripts!
AssetDatabase.Refresh();
Repaint();
}
private void DoCopyToClipboard() {
// Ensure that input focus is removed from text field.
EditorGUIUtility.keyboardControl = 0;
EditorGUIUtility.editingTextField = false;
ValidateScriptInputs();
EditorGUIUtility.systemCopyBuffer = _activeGenerator.GenerateScript(_scriptName, _ns);
}
}
}