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
|
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace Thry.ThryEditor
{
public class Presets
{
const string TAG_IS_PRESET = "isPreset";
const string TAG_POSTFIX_IS_PRESET = "_isPreset";
const string TAG_PRESET_NAME = "presetName";
static Dictionary<Material, (Material, Material)> appliedPresets = new Dictionary<Material, (Material, Material)>();
static string[] p_presetNames;
static Material[] p_presetMaterials;
static string[] presetNames { get
{
if (p_presetNames == null)
{
p_presetMaterials = AssetDatabase.FindAssets("t:material")
.Select(g => AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(g)))
.Where(m => IsPreset(m)).ToArray();
p_presetNames = p_presetMaterials.Select(m => m.GetTag(TAG_PRESET_NAME,false,m.name)).Prepend("").ToArray();
}
return p_presetNames;
}
}
private static PresetsPopupGUI window;
public static void PresetGUI(Rect r, ShaderEditor shaderEditor)
{
if(GUI.Button(r, "", Styles.icon_style_presets))
{
Event.current.Use();
if (Event.current.button == 0)
{
Vector2 pos = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
pos.x = Mathf.Min(EditorWindow.focusedWindow.position.x + EditorWindow.focusedWindow.position.width - 250, pos.x);
pos.y = Mathf.Min(EditorWindow.focusedWindow.position.y + EditorWindow.focusedWindow.position.height - 200, pos.y);
if (window != null)
window.Close();
window = ScriptableObject.CreateInstance<PresetsPopupGUI>();
window.position = new Rect(pos.x, pos.y, 250, 200);
string[] names = presetNames;
window.Init(names, p_presetMaterials, shaderEditor);
window.titleContent = new GUIContent("Preset List");
window.ShowUtility();
}
else
{
EditorUtility.DisplayCustomMenu(r, presetNames.Select(s => new GUIContent(s)).ToArray(), 0, ApplyQuickPreset, shaderEditor);
}
}
}
static void ApplyQuickPreset(object userData, string[] options, int selected)
{
Apply(p_presetMaterials[selected - 1], userData as ShaderEditor);
}
public static void PresetEditorGUI(ShaderEditor shaderEditor)
{
if (shaderEditor.IsPresetEditor)
{
EditorGUILayout.LabelField(Locale.editor.Get("preset_material_notify"), Styles.greenStyle);
string name = shaderEditor.Materials[0].GetTag(TAG_PRESET_NAME, false, "");
EditorGUI.BeginChangeCheck();
name = EditorGUILayout.TextField(Locale.editor.Get("preset_name"), name);
if (EditorGUI.EndChangeCheck())
{
shaderEditor.Materials[0].SetOverrideTag(TAG_PRESET_NAME, name);
p_presetNames = null;
}
}
if (appliedPresets.ContainsKey(shaderEditor.Materials[0]))
{
if(GUILayout.Button(Locale.editor.Get("preset_revert")+appliedPresets[shaderEditor.Materials[0]].Item1.name))
{
Revert(shaderEditor);
}
}
}
public static void Apply(Material preset, ShaderEditor shaderEditor)
{
appliedPresets[shaderEditor.Materials[0]] = (preset, new Material(shaderEditor.Materials[0]));
foreach (ShaderPart prop in shaderEditor.ShaderParts)
{
if (IsPreset(preset, prop.materialProperty))
{
prop.CopyFromMaterial(preset);
}
}
foreach (Material m in shaderEditor.Materials)
MaterialEditor.ApplyMaterialPropertyDrawers(m);
}
static void Revert(ShaderEditor shaderEditor)
{
Material key = shaderEditor.Materials[0];
Material preset = appliedPresets[key].Item1;
Material prePreset = appliedPresets[key].Item2;
foreach (ShaderPart prop in shaderEditor.ShaderParts)
{
if (IsPreset(preset, prop.materialProperty))
{
prop.CopyFromMaterial(prePreset);
}
}
foreach (Material m in shaderEditor.Materials)
MaterialEditor.ApplyMaterialPropertyDrawers(m);
appliedPresets.Remove(key);
}
public static void ApplyList(ShaderEditor shaderEditor, Material[] originals, List<Material> presets)
{
for(int i=0;i<shaderEditor.Materials.Length && i < originals.Length;i++)
shaderEditor.Materials[i].CopyPropertiesFromMaterial(originals[i]);
foreach (Material preset in presets)
{
foreach (ShaderPart prop in shaderEditor.ShaderParts)
{
if (IsPreset(preset, prop.materialProperty))
{
prop.CopyFromMaterial(preset);
}
}
}
MaterialEditor.ApplyMaterialPropertyDrawers(shaderEditor.Materials);
shaderEditor.Reload();
}
public static void SetProperty(Material m, MaterialProperty prop, bool value)
{
m.SetOverrideTag(prop.name + TAG_POSTFIX_IS_PRESET, value?"true":"");
}
public static bool IsPreset(Material m, MaterialProperty prop)
{
if (prop == null) return false;
return m.GetTag(prop.name + TAG_POSTFIX_IS_PRESET, false, "") == "true";
}
public static bool ArePreset(Material[] mats)
{
return mats.All(m => IsPreset(m));
}
public static bool IsPreset(Material m)
{
return m.GetTag(TAG_IS_PRESET, false, "false") == "true";
}
[MenuItem("Assets/Thry/Mark as preset")]
static void MarkAsPreset()
{
IEnumerable<Material> mats = Selection.assetGUIDs.Select(g => AssetDatabase.GUIDToAssetPath(g)).
Where(p => AssetDatabase.GetMainAssetTypeAtPath(p) == typeof(Material)).Select(p => AssetDatabase.LoadAssetAtPath<Material>(p));
foreach (Material m in mats)
{
m.SetOverrideTag(TAG_IS_PRESET, "true");
if (m.GetTag("presetName", false, "") == "") m.SetOverrideTag("presetName", m.name);
}
p_presetNames = null;
}
[MenuItem("Assets/Thry/Mark as preset", true)]
static bool MarkAsPresetValid()
{
return Selection.assetGUIDs.Select(g => AssetDatabase.GUIDToAssetPath(g)).
All(p => AssetDatabase.GetMainAssetTypeAtPath(p) == typeof(Material));
}
[MenuItem("Assets/Thry/Remove as preset")]
static void RemoveAsPreset()
{
IEnumerable<Material> mats = Selection.assetGUIDs.Select(g => AssetDatabase.GUIDToAssetPath(g)).
Where(p => AssetDatabase.GetMainAssetTypeAtPath(p) == typeof(Material)).Select(p => AssetDatabase.LoadAssetAtPath<Material>(p));
foreach (Material m in mats)
{
m.SetOverrideTag(TAG_IS_PRESET, "");
}
p_presetNames = null;
}
[MenuItem("Assets/Thry/Remove as preset", true)]
static bool RemoveAsPresetValid()
{
return Selection.assetGUIDs.Select(g => AssetDatabase.GUIDToAssetPath(g)).
All(p => AssetDatabase.GetMainAssetTypeAtPath(p) == typeof(Material));
}
}
public class PresetsPopupGUI : EditorWindow
{
class PresetStruct
{
public Dictionary<string,PresetStruct> dict;
string name;
Material preset;
bool isOpen = false;
bool isOn;
public PresetStruct(string name)
{
this.name = name;
dict = new Dictionary<string, PresetStruct>();
}
public PresetStruct GetSubStruct(string name)
{
name = name.Trim();
if (dict.ContainsKey(name) == false)
dict.Add(name, new PresetStruct(name));
return dict[name];
}
public void SetPreset(Material m)
{
preset = m;
}
public void StructGUI(PresetsPopupGUI popupGUI)
{
if(preset != null)
{
EditorGUI.BeginChangeCheck();
isOn = EditorGUILayout.ToggleLeft(name, isOn);
if (EditorGUI.EndChangeCheck())
{
popupGUI.ToggelPreset(preset, isOn);
}
}
if(dict.Count > 0)
{
Rect r = GUILayoutUtility.GetRect(new GUIContent(), Styles.dropDownHeader);
r.x = EditorGUI.indentLevel * 15;
r.width -= r.x;
GUI.Box(r, name, Styles.dropDownHeader);
if (Event.current.type == EventType.Repaint)
{
var toggleRect = new Rect(r.x + 4f, r.y + 2f, 13f, 13f);
EditorStyles.foldout.Draw(toggleRect, false, false, isOpen, false);
}
if (Event.current.type == EventType.MouseDown && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
{
isOpen = !isOpen;
ShaderEditor.Input.Use();
}
if (isOpen)
{
EditorGUI.indentLevel += 1;
foreach (KeyValuePair<string, PresetStruct> struc in dict)
{
struc.Value.StructGUI(popupGUI);
}
EditorGUI.indentLevel -= 1;
}
}
}
public void Reset()
{
isOn = false;
foreach (KeyValuePair<string, PresetStruct> struc in dict)
struc.Value.Reset();
}
}
Material[] beforePreset;
List<Material> tickedPresets = new List<Material>();
PresetStruct mainStruct;
ShaderEditor shaderEditor;
public void Init(string[] names, Material[] presets, ShaderEditor shaderEditor)
{
this.shaderEditor = shaderEditor;
this.beforePreset = shaderEditor.Materials.Select(m => new Material(m)).ToArray();
mainStruct = new PresetStruct("");
backgroundTextrure = new Texture2D(1,1);
if (EditorGUIUtility.isProSkin) backgroundTextrure.SetPixel(0, 0, new Color(0.18f, 0.18f, 0.18f, 1));
else backgroundTextrure.SetPixel(0, 0, new Color(0.9f, 0.9f, 0.9f, 1));
backgroundTextrure.Apply();
for (int i = 1; i < names.Length; i++)
{
string[] path = names[i].Split('/');
PresetStruct addUnder = mainStruct;
for (int j=0;j<path.Length; j++)
{
addUnder = addUnder.GetSubStruct(path[j]);
}
addUnder.SetPreset(presets[i-1]);
}
}
void ToggelPreset(Material m, bool on)
{
if (tickedPresets.Contains(m) && !on) tickedPresets.Remove(m);
if (!tickedPresets.Contains(m) && on) tickedPresets.Add(m);
Presets.ApplyList(shaderEditor, beforePreset, tickedPresets);
}
static Texture2D backgroundTextrure;
Vector2 scroll;
bool _save;
void OnGUI()
{
if (mainStruct == null) { this.Close(); return; }
GUILayout.BeginHorizontal();
scroll = GUILayout.BeginScrollView(scroll, GUILayout.Height(position.height - 55));
GUILayoutUtility.GetRect(10, 5);
TopStructGUI();
GUILayout.EndScrollView();
GUILayout.EndHorizontal();
if (GUI.Button(new Rect(5, this.position.height - 35, this.position.width / 2 - 5, 30), "Apply"))
{
_save = true;
this.Close();
}
if (GUI.Button(new Rect(this.position.width / 2, this.position.height - 35, this.position.width / 2 - 5, 30), "Discard"))
{
Revert();
}
}
private void OnDestroy()
{
if (!_save)
{
Revert();
}
}
void TopStructGUI()
{
foreach (KeyValuePair<string, PresetStruct> struc in mainStruct.dict)
{
struc.Value.StructGUI(this);
}
}
void Revert()
{
for (int i = 0; i < shaderEditor.Materials.Length; i++)
{
shaderEditor.Materials[i].CopyPropertiesFromMaterial(beforePreset[i]);
MaterialEditor.ApplyMaterialPropertyDrawers(shaderEditor.Materials[i]);
}
mainStruct.Reset();
shaderEditor.Repaint();
}
}
}
|