-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickCharacterMaterialSetter.cs
365 lines (332 loc) · 17.3 KB
/
QuickCharacterMaterialSetter.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
namespace AhabTools
{
public class QuickCharacterMaterialSetter : EditorWindow
{
#region Variables
private string newTexturesContainer = "Assets";
private string newTexturesContainerAlt = "Assets";
private bool auxVariablesFoldout = true;
private bool useSecondPath = true;
private string originalKeyword = "NX";
private string newKeyword = "N1";
private Vector2 scrollPosition;
private bool renameTextures = true;
private GUIStyle foldoutHeaderStyle;
private GUIStyle foldoutContentStyle;
#endregion
[MenuItem("Tools/Ahab Tools/Quick Character Material Setter")]
public static void ShowWindow()
{
GetWindow<QuickCharacterMaterialSetter>("Quick Character Material Setter v1.0.0.");
}
void OnGUI()
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
#region Hyperlink
GUIStyle hyperlinkStyle = new GUIStyle();
hyperlinkStyle.normal.textColor = Color.gray;
hyperlinkStyle.fontStyle = FontStyle.Italic;
Rect linkRect = EditorGUILayout.GetControlRect();
if (GUI.Button(linkRect, "Click here to visit the GitHub repository of this tool to check the latest version.", hyperlinkStyle))
{
Application.OpenURL("https://github.com/ahabdeveloper/Unity-material-textures-tools");
}
#endregion
GUILayout.Space(5);
#region How to use infobox
EditorGUILayout.HelpBox("How to Use:\n" +
"\n" +
"1. Set the path to the Main Parent Folder you wish to iterate through in search of new textures.\n" +
"\n" +
"2. If necessary, activate the 'Rename Textures' option and use it as follows:\n" +
"\n" +
" - Original Keyword: The string value you want to replace after identifying the original texture names.\n" +
" - New Keyword: The new string value you want to add.\n" +
"(For example, suppose we are creating mice of different colors. Our main material has a diffuse texture " +
"named 'Mouse_Color_X', and we want to create a new red mouse with a diffuse map called 'Mouse_Color_Red'. " +
"You would enter 'X' as the Original Keyword and 'Red' as the New Keyword.)\n" +
"\n" +
"3. Select all those materials which you want to reasign their textures in the Project Tab.\n" +
"\n" +
"4. If necessary activate a Secondary Path to lookf for textures.\n" +
"\n" +
"5. Execute the method by clicking the button!.\n" +
"\n" +
"-------------------\n" +
"\n" +
"Refill materials ALT will empty the current variables with associated textures that couldn't be refilled in the new path.", MessageType.None);
#endregion
GUILayout.Space(5);
GUILayout.Label("New Textures' Path Main Parent:");
GUILayout.BeginHorizontal();
newTexturesContainer = GUILayout.TextField(newTexturesContainer);
GUIContent buttonContent = new GUIContent("R", "Register automatically the current selected folder in the Project Tab");
if (GUILayout.Button(buttonContent, GUILayout.Width(20), GUILayout.Height(20)))
{
SetNewTexturesContainerPathToSelectedFolder();
}
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUIContent buttonContent1 = new GUIContent("Refill selected materials", "Don't forget to have the maeterials selected in the Project Tab!");
if (GUILayout.Button(buttonContent1, GUILayout.Height(40)))
{
RefillAllMaterials();
}
GUI.color = Color.red;
GUIContent buttonContent4 = new GUIContent("Refill selected materials ALT", "Only use if you know all the correct textures will be setted and you want to erase those that for any reason you wish just to empty!");
if (GUILayout.Button(buttonContent4, GUILayout.Height(40)))
{
RefillAllMaterialsAlt();
}
GUI.color = Color.white;
GUILayout.Space(15);
#region Foldout Styles
if (foldoutHeaderStyle == null)
{
foldoutHeaderStyle = new GUIStyle("ShurikenModuleTitle")
{
border = new RectOffset(2, 2, 2, 2),
fixedHeight = 22,
contentOffset = new Vector2(20f, -2f)
};
foldoutHeaderStyle.contentOffset = new Vector2(20f, -2f);
foldoutHeaderStyle.fixedWidth = 0;
}
if (foldoutContentStyle == null)
{
foldoutContentStyle = new GUIStyle(GUI.skin.box)
{
padding = new RectOffset(1, 1, 1, 1),
margin = new RectOffset(1, 1, 1, 1)
};
}
#endregion
#region Foldout drawing
Rect backgroundRect9 = GUILayoutUtility.GetRect(1f, 22f, GUILayout.ExpandWidth(true));
GUI.Box(backgroundRect9, GUIContent.none, foldoutHeaderStyle);
auxVariablesFoldout = EditorGUI.Foldout(backgroundRect9, auxVariablesFoldout, "Auxiliar Options", true, foldoutHeaderStyle);
#endregion
if (auxVariablesFoldout)
{
EditorGUI.indentLevel++;
EditorGUI.DrawRect(EditorGUILayout.BeginVertical(EditorStyles.helpBox), new Color(0.18f, 0.18f, 0.18f, 1f));
GUILayout.Label("Rename Textures?");
// Custom toggle buttons
GUILayout.BeginHorizontal();
GUIStyle activeStyle = new GUIStyle(GUI.skin.button);
activeStyle.normal.background = GUI.skin.button.active.background;
if (GUILayout.Button("On", renameTextures ? activeStyle : GUI.skin.button))
{
renameTextures = true;
}
if (GUILayout.Button("Off", !renameTextures ? activeStyle : GUI.skin.button))
{
renameTextures = false;
}
GUILayout.EndHorizontal();
if (renameTextures)
{
EditorGUILayout.HelpBox("Use the two following string variables to set those values to be renamed ", MessageType.Info);
GUILayout.Label("Original Keyword:");
originalKeyword = GUILayout.TextField(originalKeyword);
GUILayout.Label("New Keyword:");
newKeyword = GUILayout.TextField(newKeyword);
}
// Toggle for using a second path
GUILayout.Label("New Textures' Secondary Path Main Parent:");
// Custom toggle buttons
GUILayout.BeginHorizontal();
GUIStyle activeStyle1 = new GUIStyle(GUI.skin.button);
activeStyle1.normal.background = GUI.skin.button.active.background;
if (GUILayout.Button("On", useSecondPath ? activeStyle1 : GUI.skin.button))
{
useSecondPath = true;
}
if (GUILayout.Button("Off", !useSecondPath ? activeStyle1 : GUI.skin.button))
{
useSecondPath = false;
}
GUILayout.EndHorizontal();
if (useSecondPath)
{
GUILayout.Label("New Textures' Secondary Path Main Parent:");
GUILayout.BeginHorizontal();
newTexturesContainerAlt = GUILayout.TextField(newTexturesContainerAlt);
GUIContent buttonContent2 = new GUIContent("R", "Register automatically the current selected folder in the Project Tab");
if (GUILayout.Button(buttonContent2, GUILayout.Width(20), GUILayout.Height(20)))
{
SetNewTexturesContainerPathToSelectedFolderAlt();
}
GUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUI.indentLevel--;
}
GUILayout.EndScrollView();
}
#region Auxiliar Methods
private void RefillAllMaterialsAlt()
{
string[] extensions = new string[] { ".png", ".jpg", ".jpeg", ".tga", ".bmp", ".gif", ".psd", ".tif", ".tiff" }; // Supported texture formats
foreach (Object obj in Selection.objects)
{
if (obj is Material)
{
Material material = obj as Material;
Shader shader = material.shader;
Debug.Log($"Refilling textures for material: {material.name}");
int propertyCount = ShaderUtil.GetPropertyCount(shader);
for (int i = 0; i < propertyCount; i++)
{
if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv)
{
string propertyName = ShaderUtil.GetPropertyName(shader, i);
Texture texture = material.GetTexture(propertyName);
if (texture != null)
{
string textureName = texture.name;
string path = AssetDatabase.GetAssetPath(texture);
string extension = Path.GetExtension(path);
if (renameTextures && textureName.Contains(originalKeyword))
{
string oldTextureName = textureName;
textureName = textureName.Replace(originalKeyword, newKeyword);
Debug.Log($"Renaming texture from {oldTextureName} to {textureName + extension}.");
}
string[] pathsToSearch = { newTexturesContainer };
if (useSecondPath)
{
pathsToSearch = new string[] { newTexturesContainer, newTexturesContainerAlt };
}
Texture newTexture = FindTextureInPaths(pathsToSearch, textureName, extension);
if (newTexture != null)
{
material.SetTexture(propertyName, newTexture);
Debug.Log($"Updated {propertyName} with new texture {newTexture.name}");
}
else
{
Debug.LogWarning($"Failed to load new texture for {propertyName} from potential paths for material {material.name}. Detaching texture {textureName}.");
material.SetTexture(propertyName, null);
}
}
}
}
}
}
}
private void RefillAllMaterials()
{
string[] extensions = new string[] { ".png", ".jpg", ".jpeg", ".tga", ".bmp", ".gif", ".psd", ".tif", ".tiff" }; // Supported texture formats
foreach (Object obj in Selection.objects)
{
if (obj is Material)
{
Material material = obj as Material;
Shader shader = material.shader;
Debug.Log($"Refilling textures for material: {material.name}");
int propertyCount = ShaderUtil.GetPropertyCount(shader);
for (int i = 0; i < propertyCount; i++)
{
if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv)
{
string propertyName = ShaderUtil.GetPropertyName(shader, i);
Texture texture = material.GetTexture(propertyName);
if (texture != null)
{
string textureName = texture.name;
string path = AssetDatabase.GetAssetPath(texture);
string extension = Path.GetExtension(path);
if (renameTextures && textureName.Contains(originalKeyword))
{
string oldTextureName = textureName;
textureName = textureName.Replace(originalKeyword, newKeyword);
Debug.Log($"Renaming texture from {oldTextureName} to {textureName + extension}.");
}
string[] pathsToSearch = { newTexturesContainer };
if (useSecondPath)
{
pathsToSearch = new string[] { newTexturesContainer, newTexturesContainerAlt };
}
Texture newTexture = FindTextureInPaths(pathsToSearch, textureName, extension);
if (newTexture != null)
{
material.SetTexture(propertyName, newTexture);
Debug.Log($"Updated {propertyName} with new texture {newTexture.name}");
}
else
{
Debug.LogWarning($"Failed to load new texture for {propertyName} from potential paths for material {material.name}. Texture is {textureName}");
}
}
}
}
}
}
}
private Texture FindTextureInPaths(string[] paths, string textureName, string extension)
{
foreach (string path in paths)
{
string foundTexturePath = SearchDirectoryForTexture(path, textureName, extension);
if (!string.IsNullOrEmpty(foundTexturePath))
{
return AssetDatabase.LoadAssetAtPath<Texture>(foundTexturePath);
}
}
return null;
}
private string SearchDirectoryForTexture(string basePath, string textureName, string extension)
{
Queue<string> directories = new Queue<string>();
directories.Enqueue(basePath);
Debug.Log($"Starting search in base path: {basePath} for texture: {textureName + extension}");
while (directories.Count > 0)
{
string currentDirectory = directories.Dequeue();
Debug.Log($"Searching in directory: {currentDirectory}");
string[] files = Directory.GetFiles(currentDirectory, textureName + extension, SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
Debug.Log($"Found texture at: {files[0]}");
return files[0];
}
foreach (string directory in Directory.GetDirectories(currentDirectory))
{
directories.Enqueue(directory);
Debug.Log($"Enqueuing sub-directory: {directory}");
}
}
return null;
}
private void SetNewTexturesContainerPathToSelectedFolder()
{
if (Selection.activeObject && AssetDatabase.IsValidFolder(AssetDatabase.GetAssetPath(Selection.activeObject)))
{
newTexturesContainer = AssetDatabase.GetAssetPath(Selection.activeObject);
Debug.Log($"New Textures Container path set to: {newTexturesContainer}");
}
else
{
Debug.LogWarning("Please select a folder in the Project tab.");
}
}
private void SetNewTexturesContainerPathToSelectedFolderAlt()
{
if (Selection.activeObject && AssetDatabase.IsValidFolder(AssetDatabase.GetAssetPath(Selection.activeObject)))
{
newTexturesContainerAlt = AssetDatabase.GetAssetPath(Selection.activeObject);
Debug.Log($"New Textures Container path set to: {newTexturesContainerAlt}");
}
else
{
Debug.LogWarning("Please select a folder in the Project tab.");
}
}
#endregion
}
}