-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoMobileShaderSwitch.cs
199 lines (178 loc) · 6.94 KB
/
AutoMobileShaderSwitch.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
using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityStandardAssets.Utility
{
public class AutoMobileShaderSwitch : MonoBehaviour
{
[SerializeField] private ReplacementList m_ReplacementList;
// Use this for initialization
private void OnEnable()
{
#if UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_TIZEN || UNITY_STV
var renderers = FindObjectsOfType<Renderer>();
Debug.Log (renderers.Length+" renderers");
var oldMaterials = new List<Material>();
var newMaterials = new List<Material>();
int materialsReplaced = 0;
int materialInstancesReplaced = 0;
foreach(ReplacementDefinition replacementDef in m_ReplacementList.items)
{
foreach(var r in renderers)
{
Material[] modifiedMaterials = null;
for(int n=0; n<r.sharedMaterials.Length; ++n)
{
var material = r.sharedMaterials[n];
if (material.shader == replacementDef.original)
{
if (modifiedMaterials == null)
{
modifiedMaterials = r.materials;
}
if (!oldMaterials.Contains(material))
{
oldMaterials.Add(material);
Material newMaterial = (Material)Instantiate(material);
newMaterial.shader = replacementDef.replacement;
newMaterials.Add(newMaterial);
++materialsReplaced;
}
Debug.Log ("replacing "+r.gameObject.name+" renderer "+n+" with "+newMaterials[oldMaterials.IndexOf(material)].name);
modifiedMaterials[n] = newMaterials[oldMaterials.IndexOf(material)];
++materialInstancesReplaced;
}
}
if (modifiedMaterials != null)
{
r.materials = modifiedMaterials;
}
}
}
Debug.Log (materialInstancesReplaced+" material instances replaced");
Debug.Log (materialsReplaced+" materials replaced");
for(int n=0; n<oldMaterials.Count; ++n)
{
Debug.Log (oldMaterials[n].name+" ("+oldMaterials[n].shader.name+")"+" replaced with "+newMaterials[n].name+" ("+newMaterials[n].shader.name+")");
}
#endif
}
[Serializable]
public class ReplacementDefinition
{
public Shader original = null;
public Shader replacement = null;
}
[Serializable]
public class ReplacementList
{
public ReplacementDefinition[] items = new ReplacementDefinition[0];
}
}
}
namespace UnityStandardAssets.Utility.Inspector
{
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof (AutoMobileShaderSwitch.ReplacementList))]
public class ReplacementListDrawer : PropertyDrawer
{
const float k_LineHeight = 18;
const float k_Spacing = 4;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
float x = position.x;
float y = position.y;
float inspectorWidth = position.width;
// Don't make child fields be indented
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
var items = property.FindPropertyRelative("items");
var titles = new string[] {"Original", "Replacement", ""};
var props = new string[] {"original", "replacement", "-"};
var widths = new float[] {.45f, .45f, .1f};
const float lineHeight = 18;
bool changedLength = false;
if (items.arraySize > 0)
{
for (int i = -1; i < items.arraySize; ++i)
{
var item = items.GetArrayElementAtIndex(i);
float rowX = x;
for (int n = 0; n < props.Length; ++n)
{
float w = widths[n]*inspectorWidth;
// Calculate rects
Rect rect = new Rect(rowX, y, w, lineHeight);
rowX += w;
if (i == -1)
{
// draw title labels
EditorGUI.LabelField(rect, titles[n]);
}
else
{
if (props[n] == "-" || props[n] == "^" || props[n] == "v")
{
if (GUI.Button(rect, props[n]))
{
switch (props[n])
{
case "-":
items.DeleteArrayElementAtIndex(i);
items.DeleteArrayElementAtIndex(i);
changedLength = true;
break;
case "v":
if (i > 0)
{
items.MoveArrayElement(i, i + 1);
}
break;
case "^":
if (i < items.arraySize - 1)
{
items.MoveArrayElement(i, i - 1);
}
break;
}
}
}
else
{
SerializedProperty prop = item.FindPropertyRelative(props[n]);
EditorGUI.PropertyField(rect, prop, GUIContent.none);
}
}
}
y += lineHeight + k_Spacing;
if (changedLength)
{
break;
}
}
}
// add button
var addButtonRect = new Rect((x + position.width) - widths[widths.Length - 1]*inspectorWidth, y,
widths[widths.Length - 1]*inspectorWidth, lineHeight);
if (GUI.Button(addButtonRect, "+"))
{
items.InsertArrayElementAtIndex(items.arraySize);
}
y += lineHeight + k_Spacing;
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SerializedProperty items = property.FindPropertyRelative("items");
float lineAndSpace = k_LineHeight + k_Spacing;
return 40 + (items.arraySize*lineAndSpace) + lineAndSpace;
}
}
#endif
}