Example #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);
            try
            {
                SerializedProperty layerUsageProp      = property.FindPropertyRelative(ChiselBrushMaterial.kLayerUsageFieldName);
                SerializedProperty renderMaterialProp  = property.FindPropertyRelative(ChiselBrushMaterial.kRenderMaterialFieldName);
                SerializedProperty physicsMaterialProp = property.FindPropertyRelative(ChiselBrushMaterial.kPhysicsMaterialFieldName);

                // Don't make child fields be indented
                var originalPosition = position;
                position = EditorGUI.IndentedRect(position);
                var indentOffset = position.x - originalPosition.x;

                var showMaterial = (ChiselEditorUtility.GetContextWidth() > 430);

                var physicMaterialPrefixRect = originalPosition;
                physicMaterialPrefixRect.height = EditorGUI.GetPropertyHeight(SerializedPropertyType.ExposedReference, GUIContent.none);
                originalPosition.yMin          += physicMaterialPrefixRect.height + kSpacing;

                var previewSize        = originalPosition.height;
                var materialPrefixRect = originalPosition;
                materialPrefixRect.height = EditorGUI.GetPropertyHeight(SerializedPropertyType.ExposedReference, GUIContent.none);
                originalPosition.yMin    += materialPrefixRect.height + kSpacing;

                var materialPreviewRect = materialPrefixRect;
                materialPreviewRect.width  = previewSize;
                materialPreviewRect.height = previewSize;

                var layerUsagePropRect = originalPosition;

                EditorGUI.BeginChangeCheck();
                {
                    {
                        var materialLabelID  = EditorGUIUtility.GetControlID(kBrushMaterialEditorHashCode, FocusType.Keyboard, materialPrefixRect);
                        var materialPropRect = EditorGUI.PrefixLabel(materialPrefixRect, materialLabelID, kRenderMaterialContents);
                        materialPreviewRect.x  = materialPropRect.x;
                        materialPropRect.xMin -= indentOffset;
                        if (showMaterial)
                        {
                            materialPropRect.xMin += previewSize + kSpacing;
                        }
                        layerUsagePropRect.xMin = materialPropRect.xMin;
                        EditorGUI.PropertyField(materialPropRect, renderMaterialProp, GUIContent.none, false);
                    }

                    {
                        EditorGUI.PropertyField(layerUsagePropRect, layerUsageProp, GUIContent.none, false);
                    }

                    {
                        var physicMaterialLabelID  = EditorGUIUtility.GetControlID(kBrushPhysicMaterialEditorHashCode, FocusType.Keyboard, physicMaterialPrefixRect);
                        var physicMaterialPropRect = EditorGUI.PrefixLabel(physicMaterialPrefixRect, physicMaterialLabelID, kPhysicRenderMaterialContents);

                        physicMaterialPropRect.xMin -= indentOffset;
                        EditorGUI.PropertyField(physicMaterialPropRect, physicsMaterialProp, GUIContent.none, false);
                    }

                    // Render material preview
                    if (showMaterial)
                    {
                        if (styles == null)
                        {
                            styles = new Styles();
                        }
                        OnPreviewGUI(materialPreviewRect, styles.background, renderMaterialProp.objectReferenceValue as Material);
                    }
                }
                if (EditorGUI.EndChangeCheck())
                {
                    property.serializedObject.ApplyModifiedProperties();
                }
            }
            catch (ExitGUIException) { }
            catch (Exception ex) { Debug.LogException(ex); }

            EditorGUI.EndProperty();
        }
Example #2
0
        public override void OnInspectorGUI()
        {
            try
            {
                const float spacing = 2;
                EditorGUI.BeginChangeCheck();
                {
                    EditorGUILayout.PropertyField(physicsMaterialProp, true);

                    if (!ShowMaterialIcon)
                    {
                        float materialPropHeight = EditorGUI.GetPropertyHeight(SerializedPropertyType.ExposedReference, GUIContent.none);

                        var materialPropRect = EditorGUILayout.GetControlRect(GUILayout.Height(materialPropHeight));
                        EditorGUI.PropertyField(materialPropRect, renderMaterialProp, RenderMaterialContents, true);

                        var layerUsagePropRect = EditorGUILayout.GetControlRect(GUILayout.Height(materialPropHeight * 2));
                        EditorGUI.PropertyField(layerUsagePropRect, layerUsageProp, LayerUsageContents, true);
                    }
                    else
                    {
                        #if UNITY_2017_1_OR_ABOVE
                        float lineHeight = EditorGUILayout.singleLineHeight;
                        #else
                        float lineHeight = 16;
                        #endif
                        float materialSize = 3 * lineHeight;

                        // Calculate rects
                        var materialPart        = EditorGUILayout.GetControlRect(GUILayout.Height(materialSize));
                        var materialLabelID     = EditorGUIUtility.GetControlID(SurfaceEditorHashCode, FocusType.Keyboard, materialPart);
                        var materialPropRect    = EditorGUI.PrefixLabel(materialPart, materialLabelID, RenderMaterialContents);
                        var materialPreviewRect = materialPropRect;
                        var showMaterial        = (ChiselEditorUtility.GetContextWidth() > 320);

                        float materialPropHeight = EditorGUI.GetPropertyHeight(SerializedPropertyType.ExposedReference, GUIContent.none);
                        if (showMaterial)
                        {
                            materialPreviewRect.width = materialSize;
                            materialPropRect.x       += materialSize + spacing;
                            materialPropRect.width   -= materialSize + spacing;
                        }

                        var layerUsagePropRect = materialPropRect;
                        layerUsagePropRect.y   += materialPropHeight;
                        materialPropRect.height = materialPropHeight;

                        // Show material prop
                        var prevIndentLevel = EditorGUI.indentLevel;
                        EditorGUI.indentLevel = 0;
                        {
                            EditorGUI.PropertyField(materialPropRect, renderMaterialProp, GUIContent.none, true);
                            EditorGUI.PropertyField(layerUsagePropRect, layerUsageProp, GUIContent.none, true);
                        }
                        EditorGUI.indentLevel = prevIndentLevel;

                        // Render material preview
                        if (showMaterial)
                        {
                            if (styles == null)
                            {
                                styles = new Styles();
                            }
                            OnPreviewGUI(materialPreviewRect, styles.background);
                        }
                    }
                }
                if (EditorGUI.EndChangeCheck())
                {
                    serializedObject.ApplyModifiedProperties();
                }
            }
            catch (ExitGUIException) { }
            catch (Exception ex) { Debug.LogException(ex); }
        }