-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptableObjectsCreatorWindow.cs
More file actions
167 lines (142 loc) · 5.7 KB
/
Copy pathScriptableObjectsCreatorWindow.cs
File metadata and controls
167 lines (142 loc) · 5.7 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
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System;
namespace OmriOvadia.Utils
{
public class ScriptableObjectsCreatorWindow : EditorWindow
{
private string scriptName = "NewScriptableObject";
private string fileName = "NewCustomScriptableObject";
private string menuName = "ScriptableObjects/CustomScriptableObject";
private string savePath = "Assets/Scripts/";
private List<Field> fields = new List<Field>();
[MenuItem("Tools/ScriptableObjects Creator")]
public static void ShowWindow()
{
GetWindow<ScriptableObjectsCreatorWindow>("ScriptableObjects Creator");
}
void OnGUI()
{
RenderFields();
RenderButtons();
}
private void RenderFields()
{
GUILayout.Label("Create a new ScriptableObject script", EditorStyles.boldLabel);
scriptName = EditorGUILayout.TextField("Script Name", scriptName);
fileName = EditorGUILayout.TextField("File Name", fileName);
menuName = EditorGUILayout.TextField("Menu Name", menuName);
savePath = EditorGUILayout.TextField("Save Path", savePath);
}
private void RenderButtons()
{
if (GUILayout.Button("Add Field"))
{
fields.Add(new Field());
}
for (int i = 0; i < fields.Count; i++)
{
RenderField(i);
}
if (GUILayout.Button("Create Script"))
{
TryCreateScriptableObjectScript();
}
}
private void RenderField(int index)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Type", GUILayout.Width(50));
fields[index].type = EditorGUILayout.TextField(fields[index].type, GUILayout.Width(100));
GUILayout.Label("Name", GUILayout.Width(50));
fields[index].name = EditorGUILayout.TextField(fields[index].name, GUILayout.Width(100));
GUILayout.Label("Public", GUILayout.Width(50));
fields[index].isPublic = EditorGUILayout.Toggle(fields[index].isPublic, GUILayout.Width(100));
if (GUILayout.Button("Remove"))
{
fields.RemoveAt(index);
}
EditorGUILayout.EndHorizontal();
}
private void TryCreateScriptableObjectScript()
{
string scriptFilePath = Path.Combine(savePath, scriptName + ".cs");
if (IsScriptNameDuplicate(scriptFilePath))
{
EditorUtility.DisplayDialog("Error", "A script with the same name already exists.", "OK");
return;
}
if (IsMenuNameDuplicate(menuName))
{
EditorUtility.DisplayDialog("Error", "A ScriptableObject with the same menuName already exists.", "OK");
return;
}
CreateScriptableObjectScript();
}
private void CreateScriptableObjectScript()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("using UnityEngine;");
sb.AppendLine("");
sb.AppendLine("[CreateAssetMenu(fileName = \"" + fileName + "\", menuName = \"" + menuName + "\")]");
sb.AppendLine("public class " + scriptName + " : ScriptableObject");
sb.AppendLine("{");
foreach (var field in fields)
{
sb.AppendLine(" " + (field.isPublic ? "public" : "private") + " " + field.type + " " + field.name + ";");
}
sb.AppendLine("}");
WriteScriptToFile(sb.ToString());
}
private void WriteScriptToFile(string scriptContent)
{
string path = Path.Combine(savePath, scriptName + ".cs");
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
File.WriteAllText(path, scriptContent);
AssetDatabase.Refresh();
}
private bool IsMenuNameDuplicate(string menuNameToCheck)
{
string[] allScripts = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories);
string pattern = @"\[CreateAssetMenu\(.*menuName\s*=\s*""" + Regex.Escape(menuNameToCheck) + @""".*\)\]";
foreach (string scriptPath in allScripts)
{
string scriptContent = File.ReadAllText(scriptPath);
if (Regex.IsMatch(scriptContent, pattern))
{
Debug.Log("Duplicate menuName found in script: " + scriptPath);
return true;
}
}
return false;
}
private bool IsScriptNameDuplicate(string scriptFilePath)
{
string scriptFileName = Path.GetFileName(scriptFilePath);
string[] allScripts = Directory.GetFiles(Application.dataPath, scriptFileName, SearchOption.AllDirectories);
foreach (string scriptPath in allScripts)
{
// Check if the found script's name matches the intended new script's name
if (Path.GetFileName(scriptPath).Equals(scriptFileName, StringComparison.OrdinalIgnoreCase))
{
Debug.Log("Duplicate script name found: " + scriptPath);
return true;
}
}
return false;
}
class Field
{
public string type;
public string name;
public bool isPublic = true;
}
}
}