Tanoda
pb_MeshRendererEditor.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3using System.Collections;
4using System.Reflection;
6
7namespace GILES.Interface
8{
10 {
11 private MeshRenderer _meshRenderer;
12 private Material[] _materials;
13
14 protected override void InitializeGUI()
15 {
16 _meshRenderer = (MeshRenderer) target;
17
18 pb_GUIUtility.AddVerticalLayoutGroup(gameObject);
19
20 pb_TypeInspector enabled_inspector = pb_InspectorResolver.GetInspector(typeof(bool));
21
22 enabled_inspector.Initialize("Enabled", UpdateEnabled, OnSetEnabled);
23 enabled_inspector.onValueBeginChange = () => { Undo.RegisterState( new UndoReflection(_meshRenderer, "enabled"), "MeshRenderer Enabled" ); };
24 enabled_inspector.transform.SetParent(transform);
25
26
27 _materials = _meshRenderer.materials;
28#if !UNITY_WEBGL
29 if (_materials != null)
30 {
31 /* //Change Shader to double sided
32 var buttonGO = new GameObject("ClickButton");
33 var image = buttonGO.AddComponent<Image>();
34 var button = buttonGO.AddComponent<Button>();
35 var textGO = new GameObject("LabelGO");
36 var text = textGO.AddComponent<Text>();
37 textGO.GetComponent<RectTransform>().sizeDelta = new Vector2(128, 14);
38 text.font = Font.CreateDynamicFontFromOSFont("Arial", 24);
39 text.alignment = TextAnchor.MiddleCenter;
40 text.text = "DoubleSided";
41 text.color = Color.white;
42 text.resizeTextForBestFit = true;
43 text.resizeTextMaxSize = 20;
44 textGO.transform.SetParent(buttonGO.transform);
45 image.sprite = GameObject.Find("SaveButton").GetComponent<Image>().sprite;
46 image.type = Image.Type.Sliced;
47 image.color = new Color(255, 255, 255, 40 / 255.0f);
48 buttonGO.transform.SetParent(transform);
49 button.onClick.AddListener(ChangeToDoubleSidedShader);
50 */
51 int i = 0;
52 foreach (var _material in _materials)
53 {
54 pb_TypeInspector metallic = pb_InspectorResolver.GetInspector(typeof(float));
55 pb_TypeInspector smoothness = pb_InspectorResolver.GetInspector(typeof(float));
56 pb_RawImageInspector texture = (pb_RawImageInspector)pb_InspectorResolver.GetInspector(typeof(RawImage));
57 pb_RawImageInspector metallicMap = (pb_RawImageInspector)pb_InspectorResolver.GetInspector(typeof(RawImage));
58 pb_RawImageInspector bumpMap = (pb_RawImageInspector)pb_InspectorResolver.GetInspector(typeof(RawImage));
59 pb_RawImageInspector occlusionMap = (pb_RawImageInspector)pb_InspectorResolver.GetInspector(typeof(RawImage));
60 pb_TypeInspector color = pb_InspectorResolver.GetInspector(typeof(Color));
61
62 pb_GUIUtility.CreateLabel("Material " + i).transform.SetParent(transform);;
63
64 if (_material.HasProperty("_Metallic"))
65 {
66 metallic.SetIndexInCollection(i);
67 metallic.Initialize("Metallic", GetStoredMetallicValue, OnSetMetallicValue);
68 metallic.transform.SetParent(transform);
69 }
70
71 if (_material.HasProperty("_Glossiness"))
72 {
73 smoothness.SetIndexInCollection(i);
74 smoothness.Initialize("Smoothness", GetStoredSmoothnessValue, OnSetSmoothnessValue);
75 smoothness.transform.SetParent(transform);
76 }
77
78
79 if (_material.HasProperty("_Color"))
80 {
81 color.SetIndexInCollection(i);
82 color.Initialize("Color", GetStoredColorValue, OnSetColorValue);
83 color.transform.SetParent(transform);
84 }
85
86 texture.SetIndexInCollection(i);
87 texture.Initialize("Texture", GetStoredImageValue, OnSetImageValue);
88 texture.transform.SetParent(transform);
89
90 if (_material.HasProperty("_MainTex"))
91 texture.ui_image.texture = _material.GetTexture("_MainTex");
92
93 if (_material.HasProperty("_MetallicGlossMap"))
94 {
95 metallicMap.SetIndexInCollection(i);
96 metallicMap.Initialize("Metallic", GetStoredMetallicImageValue, OnSetMetallicImageValue);
97 metallicMap.transform.SetParent(transform);
98 metallicMap.ui_image.texture = _material.GetTexture("_MetallicGlossMap");
99 }
100
101 if (_material.HasProperty("_BumpMap"))
102 {
103 bumpMap.SetIndexInCollection(i);
104 bumpMap.Initialize("Normal", GetStoredNormalImageValue, OnSetNormalImageValue);
105 bumpMap.transform.SetParent(transform);
106 bumpMap.ui_image.texture = _material.GetTexture("_BumpMap");
107 }
108
109
110 if (_material.HasProperty("_OcclusionMap"))
111 {
112 occlusionMap.SetIndexInCollection(i);
113 occlusionMap.Initialize("Occlusion", GetStoredOcclusionImageValue, OnSetOcclusionImageValue);
114 occlusionMap.transform.SetParent(transform);
115 occlusionMap.ui_image.texture = _material.GetTexture("_OcclusionMap");
116 }
117 i++;
118 }
119 }
120
121#endif
122
123 }
124
125 void ChangeToDoubleSidedShader()
126 {
127 ShaderChanger.Change(_materials, "Ciconia Studio/Double Sided/Standard/Diffuse Bump");
128 }
129
130 object GetStoredColorValue(int index)
131 {
132 return _materials[index].GetColor("_Color");
133 }
134
135 void OnSetColorValue(int index, object value)
136 {
137 foreach (var go in pb_Selection.gameObjects)
138 {
139 var mr = go.GetComponent<MeshRenderer>();
140 if (mr == null) continue;
141 var mats = mr.materials;
142 if (mats == null) continue;
143 if (mats.Length - 1 >= index)
144 {
145 var cth = go.DemandComponent<ChangedTextureHolder>();
146 var matSettings = cth.matSettings.GetIndex(index);
147 matSettings.color = (Color)value;
148 cth.matSettings[index] = matSettings;
149
150 mats[index].color = (Color)value;
151 }
152 }
153 }
154 object GetStoredImageValue(int index)
155 {
156 return _materials[index].GetTexture("_MainTex");
157 }
158
159 void OnSetImageValue(int index, object value)
160 {
161 if (value is RawImage)
162 if (_materials[index].HasProperty("_MainTex"))
163 {
164 _materials[index].SetTexture("_MainTex", ((RawImage)value).mainTexture);
165 }
166 if (value is string)
167 {
168 var cth = _meshRenderer.gameObject.DemandComponent<ChangedTextureHolder>();
169 var matSettings = cth.matSettings.GetIndex(index);
170 matSettings.textureName = (string)value;
171 cth.matSettings[index] = matSettings;
172 }
173 }
174
175
176 object GetStoredMetallicImageValue(int index)
177 {
178 return _materials[index].GetTexture("_MetallicGlossMap");
179 }
180
181 void OnSetMetallicImageValue(int index, object value)
182 {
183 if (value is RawImage)
184 if (_materials[index].HasProperty("_MetallicGlossMap"))
185 {
186 _materials[index].SetTexture("_MetallicGlossMap", ((RawImage)value).mainTexture);
187 }
188 if (value is string)
189 {
190 var cth = _meshRenderer.gameObject.DemandComponent<ChangedTextureHolder>();
191 var matSettings = cth.matSettings.GetIndex(index);
192 matSettings.metallicName = (string)value;
193 cth.matSettings[index] = matSettings;
194 }
195 }
196 object GetStoredNormalImageValue(int index)
197 {
198 var texture = _materials[index].GetTexture("_BumpMap");
199 if (texture == null) texture = new Texture2D(1,1);
200 return texture;
201 }
202
203 void OnSetNormalImageValue(int index, object value)
204 {
205 if (value is RawImage)
206 if (_materials[index].HasProperty("_BumpMap"))
207 {
208 _materials[index].SetTexture("_BumpMap", ((RawImage)value).mainTexture);
209 }
210 if (value is string)
211 {
212 var cth = _meshRenderer.gameObject.DemandComponent<ChangedTextureHolder>();
213 var matSettings = cth.matSettings.GetIndex(index);
214 matSettings.normalName = (string)value;
215 cth.matSettings[index] = matSettings;
216 }
217 }
218 object GetStoredOcclusionImageValue(int index)
219 {
220 return _materials[index].GetTexture("_OcclusionMap");
221 }
222
223 void OnSetOcclusionImageValue(int index, object value)
224 {
225 if (value is RawImage)
226 if (_materials[index].HasProperty("_OcclusionMap"))
227 {
228 _materials[index].SetTexture("_OcclusionMap", ((RawImage)value).mainTexture);
229 }
230 if (value is string)
231 {
232 var cth = _meshRenderer.gameObject.DemandComponent<ChangedTextureHolder>();
233 var matSettings = cth.matSettings.GetIndex(index);
234 matSettings.occlusionName = (string)value;
235 cth.matSettings[index] = matSettings;
236 }
237 }
238 object GetStoredMetallicValue(int index)
239 {
240 return _materials[index].GetFloat("_Metallic");
241 }
242
243 void OnSetMetallicValue(int index, object value)
244 {
245 var cth = _meshRenderer.gameObject.DemandComponent<ChangedTextureHolder>();
246 var matSettings = cth.matSettings.GetIndex(index);
247 matSettings.metallic = (float)value;
248 cth.matSettings[index] = matSettings;
249
250 _materials[index].SetFloat("_Metallic", (float)value);
251 }
252 object GetStoredSmoothnessValue(int index)
253 {
254 return _materials[index].GetFloat("_Glossiness");
255 }
256
257 void OnSetSmoothnessValue(int index, object value)
258 {
259 foreach (var go in pb_Selection.gameObjects)
260 {
261 var mr = go.GetComponent<MeshRenderer>();
262 if (mr == null) continue;
263 var mats = mr.materials;
264 if (mats == null) continue;
265 if (mats.Length - 1 >= index)
266 mats[index].SetFloat("_Glossiness", (float)value);
267 }
268 }
269
270 object UpdateEnabled()
271 {
272 return _meshRenderer.enabled;
273 }
274
275 void OnSetEnabled(object value)
276 {
277 _meshRenderer.enabled = (bool) value;
278 pb_ComponentDiff.AddDiff(target, "enabled", _meshRenderer.enabled);
279 }
280 }
281}
UnityEngine.Color Color
Definition: TestScript.cs:32
List< MatSettings > matSettings
Component target
The UnityEngine.Component being edited.
static void AddDiff(Component component, string name, object value)
static void RegisterState(IUndo target, string message)
Definition: Undo.cs:214
static void Change(Material mat, string shaderName)
Definition: ShaderChanger.cs:7