public static void Draw(SerializedProperty sSlots, Shader shader) { var removeIndex = -1; for (var i = 0; i < sSlots.arraySize; i++) { var sSlot = sSlots.GetArrayElementAtIndex(i); var sName = sSlot.FindPropertyRelative("Name"); var sAlias = sSlot.FindPropertyRelative("Alias"); EditorGUILayout.BeginVertical(EditorStyles.helpBox); P3dHelper.BeginColor(P3dHelper.TexEnvNameExists(shader, sName.stringValue) == false); { var rect = P3dHelper.Reserve(); var rectA = rect; rectA.width = EditorGUIUtility.labelWidth; var rectB = rect; rectB.xMin += EditorGUIUtility.labelWidth; rectB.xMax -= 20; var rectC = rect; rectC.xMin = rectC.xMax - 18; EditorGUI.LabelField(rectA, new GUIContent(sName.name, sName.tooltip)); EditorGUI.PropertyField(rectB, sName, GUIContent.none); // Draw menu if (GUI.Button(rectC, "", EditorStyles.popup) == true) { var menu = new GenericMenu(); var texEnvs = P3dHelper.GetTexEnvs(shader); if (texEnvs != null && texEnvs.Count > 0) { for (var j = 0; j < texEnvs.Count; j++) { var texName = texEnvs[j].Name; menu.AddItem(new GUIContent(texName), sName.stringValue == texName, () => { sName.stringValue = texName; sName.serializedObject.ApplyModifiedProperties(); }); } } else { menu.AddDisabledItem(new GUIContent("This shader has no textures!")); } menu.DropDown(rectC); } } P3dHelper.EndColor(); EditorGUILayout.PropertyField(sAlias); DrawElement(sSlot.FindPropertyRelative("WriteR")); DrawElement(sSlot.FindPropertyRelative("WriteG")); DrawElement(sSlot.FindPropertyRelative("WriteB")); DrawElement(sSlot.FindPropertyRelative("WriteA")); P3dHelper.BeginColor(Color.red); if (GUILayout.Button("Remove Slot") == true) { removeIndex = i; } P3dHelper.EndColor(); EditorGUILayout.EndVertical(); } EditorGUILayout.Separator(); if (GUILayout.Button("Add Slot") == true) { sSlots.arraySize += 1; } if (removeIndex >= 0) { sSlots.DeleteArrayElementAtIndex(removeIndex); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var sObj = property.serializedObject; var sIdx = property.FindPropertyRelative("Index"); var sNam = property.FindPropertyRelative("Name"); var rectA = position; rectA.width = EditorGUIUtility.labelWidth; var rectB = position; rectB.xMin += EditorGUIUtility.labelWidth; rectB.width = 20; var rectC = position; rectC.xMin += EditorGUIUtility.labelWidth; rectC.xMin += 22; rectC.width -= 20; var rectD = position; rectD.xMin = rectD.xMax - 18; var component = property.serializedObject.targetObject as Component; var paintable = component != null?component.GetComponentInParent <P3dPaintable>() : null; var missing = true; // Valid slot? if (paintable != null) { var material = P3dHelper.GetMaterial(paintable.CachedRenderer, sIdx.intValue); if (material != null && P3dHelper.TexEnvNameExists(material.shader, sNam.stringValue) == true) { missing = false; } } P3dEditor.BeginError(missing); { EditorGUI.LabelField(rectA, label); sObj.Update(); sIdx.intValue = Mathf.Clamp(EditorGUI.IntField(rectB, sIdx.intValue), 0, 99); sNam.stringValue = EditorGUI.TextField(rectC, sNam.stringValue); sObj.ApplyModifiedProperties(); // Draw menu if (GUI.Button(rectD, "", EditorStyles.popup) == true) { var menu = new GenericMenu(); if (paintable != null) { var materials = paintable.CachedRenderer.sharedMaterials; if (materials.Length > 0) { for (var i = 0; i < materials.Length; i++) { var material = materials[i]; var materialName = i.ToString(); var matIndex = i; if (material != null) { materialName += " (" + material.name + ")"; var texEnvs = P3dHelper.GetTexEnvs(material.shader); if (texEnvs != null && texEnvs.Count > 0) { foreach (var texEnv in texEnvs) { var texName = texEnv.Name; var texTitle = texEnv.Title; var tex = material.GetTexture(texName); if (tex != null) { texTitle += " (" + tex.name + ")"; } else { texTitle += " (empty)"; } menu.AddItem(new GUIContent(materialName + "/" + texTitle), sIdx.intValue == matIndex && sNam.stringValue == texName, () => { sObj.Update(); sIdx.intValue = matIndex; sNam.stringValue = texName; sObj.ApplyModifiedProperties(); }); } } else { menu.AddDisabledItem(new GUIContent(materialName + "/This Material's shader has no textures!")); } } else { menu.AddDisabledItem(new GUIContent(materialName + "/This Material is null!")); } } } else { menu.AddDisabledItem(new GUIContent("This GameObject has no materials!")); } } menu.DropDown(rectD); } } P3dEditor.EndError(); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var sObj = property.serializedObject; var sIdx = property.FindPropertyRelative("Index"); var sNam = property.FindPropertyRelative("Name"); var right = position; right.xMin += EditorGUIUtility.labelWidth; var rightA = new Rect(right.xMin, right.y, 20.0f, right.height); var rightB = new Rect(right.xMin + 25.0f, right.y, right.width - 50.0f, right.height); var rightC = new Rect(right.xMax - 20.0f, right.y, 20.0f, right.height); var component = property.serializedObject.targetObject as Component; var exists = false; // Invalid slot? if (component != null) { var material = P3dHelper.GetMaterial(component.gameObject, sIdx.intValue); if (P3dHelper.TexEnvNameExists(material, sNam.stringValue) == false) { exists = true; } } P3dHelper.BeginColor(exists); { EditorGUI.LabelField(position, label); sIdx.intValue = Mathf.Clamp(EditorGUI.IntField(rightA, sIdx.intValue), 0, 99); sNam.stringValue = EditorGUI.TextField(rightB, sNam.stringValue); sObj.ApplyModifiedProperties(); // Draw menu if (GUI.Button(rightC, "", EditorStyles.popup) == true) { if (component != null) { var menu = new GenericMenu(); var renderer = component.GetComponent <Renderer>(); if (renderer != null) { var materials = renderer.sharedMaterials; if (materials.Length > 0) { for (var i = 0; i < materials.Length; i++) { var material = materials[i]; var materialName = i.ToString(); var matIndex = i; if (material != null) { materialName += " (" + material.name + ")"; var texEnvs = P3dHelper.GetTexEnvs(material); if (texEnvs != null && texEnvs.Count > 0) { for (var j = 0; j < texEnvs.Count; j++) { var texName = texEnvs[j].Name; var texTitle = texName; var tex = material.GetTexture(texName); if (tex != null) { texTitle += " (" + tex.name + ")"; } else { texTitle += " (empty)"; } menu.AddItem(new GUIContent(materialName + "/" + texTitle), sIdx.intValue == matIndex && sNam.stringValue == texName, () => { sIdx.intValue = matIndex; sNam.stringValue = texName; sObj.ApplyModifiedProperties(); }); } } else { menu.AddDisabledItem(new GUIContent(materialName + "/This Material's shader has no textures!")); } } else { menu.AddDisabledItem(new GUIContent(materialName + "/This Material is null!")); } } } else { menu.AddDisabledItem(new GUIContent("This GameObject has no materials!")); } } else { menu.AddDisabledItem(new GUIContent("This GameObject has no renderer!")); } menu.DropDown(rightC); } } } P3dHelper.EndColor(); }