-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTextureAssign.cs
210 lines (170 loc) · 6.4 KB
/
AutoTextureAssign.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
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
[Serializable]
public class AssignData
{
public string propertyName;
public string textureName;
}
public class AssignDataVisualElement : VisualElement
{
public AssignDataVisualElement()
{
var root = new VisualElement();
var propertyName = new TextField { name = "propertyName", label = "Property Name", tooltip = "Property name that define in the shader" };
var textureName = new TextField { name = "textureName", label = "Texture Name", tooltip = "Search filer will be material name + input string" };
root.Add(propertyName);
root.Add(textureName);
Add(root);
}
}
public class AutoTextureAssign : EditorWindow
{
List<AssignData> assignData = new List<AssignData>();
string findingDirectory = "Assets/";
[MenuItem("Tools/Auto Texture Assign")]
public static void ShowWindow()
{
AutoTextureAssign wnd = GetWindow<AutoTextureAssign>();
wnd.titleContent = new GUIContent("AutoTextureAssign");
}
private void CreateGUI()
{
VisualElement root = rootVisualElement;
Func<VisualElement> makeItem = () =>
{
var item = new AssignDataVisualElement();
var propertyName = item.Q<TextField>("propertyName");
var textureName = item.Q<TextField>("textureName");
propertyName.RegisterValueChangedCallback(evt =>
{
var i = (int)propertyName.userData;
var data = assignData[i];
data.propertyName = evt.newValue.ToString();
});
textureName.RegisterValueChangedCallback(evt =>
{
var i = (int)textureName.userData;
var data = assignData[i];
data.textureName = evt.newValue.ToString();
});
return item;
};
Action<VisualElement, int> bindItem = (e, i) =>
{
var dataItem = e as AssignDataVisualElement;
var propertyName = dataItem.Q<TextField>("propertyName");
var textureName = dataItem.Q<TextField>("textureName");
var data = assignData[i];
propertyName.userData = i;
textureName.userData = i;
};
var pathText = new TextField {name = "pathText", label = "Search Path", value = "Assets/" };
var listView = new ListView(assignData, 40, makeItem, bindItem);
listView.showBorder = true;
listView.headerTitle = "Assign Data";
listView.showFoldoutHeader = true;
listView.showAddRemoveFooter = true;
var assignButton = new Button { name = "assignButton", text = "Assign", tooltip = "Auto assign selected materials" };
assignButton.clicked += Click;
root.Add(pathText);
root.Add(listView);
root.Add(assignButton);
pathText.RegisterValueChangedCallback(evt =>
{
findingDirectory = evt.newValue;
});
listView.Q<Button>("unity-list-view__add-button").clickable = new Clickable(() =>
{
listView.itemsSource.Add(new AssignData());
listView.RefreshItems();
});
listView.Q<Button>("unity-list-view__remove-button").clickable = new Clickable(() =>
{
if (listView.selectedIndex < 0)
{
listView.itemsSource.RemoveAt(assignData.Count - 1);
}
else
{
listView.itemsSource.RemoveAt(listView.selectedIndex);
}
listView.RefreshItems();
});
}
void Click()
{
var selection = Selection.objects;
UpgradeSelection(selection, assignData);
}
public void UpgradeSelection(UnityEngine.Object[] selection, List<AssignData> assignData)
{
if (selection != null)
{
if ((!Application.isBatchMode) && (!EditorUtility.DisplayDialog("Material Upgrader", "The upgrade will overwrite materials in your project. " + "Make sure to have a project backup before proceeding", "Proceed", "Cancel")))
return;
int totalMaterialCount = 0;
foreach (var obj in selection)
{
if (obj.GetType() == typeof(Material))
totalMaterialCount++;
}
int materialIndex = 0;
foreach (var obj in selection)
{
if (obj.GetType() == typeof(Material))
{
materialIndex++;
if (UnityEditor.EditorUtility.DisplayCancelableProgressBar("Updating", string.Format("({0} of {1}) {2}", materialIndex, totalMaterialCount, obj.name), (float)materialIndex / (float)totalMaterialCount))
break;
Material m = obj as Material;
Assign(m, assignData);
}
}
UnityEditor.EditorUtility.ClearProgressBar();
SaveAssetsAndFreeMemory();
}
}
void Assign(Material m, List<AssignData> assignData)
{
foreach (var data in assignData)
{
if (!m.HasTexture(data.propertyName))
continue;
var texture = SearchTexture(m.name, data.textureName);
if (texture == null)
continue;
m.SetTexture(data.propertyName, texture);
}
}
private Texture SearchTexture(string materialName, string name)
{
string[] pathArray = new string[1];
pathArray.SetValue(findingDirectory, 0);
var GUIDs = AssetDatabase.FindAssets(materialName + " " + name + " " + " t=Texture", pathArray);
if (GUIDs.Length <= 0)
return null;
var assetsPaths = new List<string>();
foreach (var Guid in GUIDs)
{
assetsPaths.Add(AssetDatabase.GUIDToAssetPath(Guid));
}
var textures = new List<Texture>();
foreach (var path in assetsPaths)
{
textures.Add(AssetDatabase.LoadAssetAtPath<Texture>(path));
}
var texture = AssetDatabase.LoadAssetAtPath<Texture>(AssetDatabase.GUIDToAssetPath(GUIDs[0])); ;
return texture;
}
static void SaveAssetsAndFreeMemory()
{
AssetDatabase.SaveAssets();
GC.Collect();
EditorUtility.UnloadUnusedAssetsImmediate();
AssetDatabase.Refresh();
}
}