HI there,
I’m facing an issue related to spacing element when i use the Foldout property.
When i fold my element, the top elements override the one below them, and i’m wondering how to fix that, how the keep the spacing relative, should i use some kind of box gui or something like that ?
Here’s the script i’ve made
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class VariableReferenceEditor : PropertyDrawer
{
SerializedProperty useConstant;
SerializedProperty constantValue;
SerializedProperty variable;
public virtual UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
{
return null;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
//base.OnGUI(position, property, label);
EditorGUI.BeginProperty(position, label, property);
Rect fieldRect = new Rect(position);
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
property.isExpanded = EditorGUI.Foldout(fieldRect, property.isExpanded, new GUIContent(property.name), true);
if(!property.isExpanded)
{
return;
}
++indent;
Rect constanctRect = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight);
Rect valueRect = new Rect(position.x, position.y + (EditorGUIUtility.singleLineHeight * 2), position.width, EditorGUIUtility.singleLineHeight);
useConstant = property.FindPropertyRelative("UseConstant");
constantValue = property.FindPropertyRelative("ConstantValue");
variable = property.FindPropertyRelative("Variable");
useConstant.boolValue = EditorGUI.Toggle(constanctRect, "Use constant", useConstant.boolValue);
if(useConstant.boolValue)
{
EditorGUI.PropertyField(valueRect, constantValue, new GUIContent("Constant Value"));
}
else
{
variable.objectReferenceValue = GetObjectField(valueRect, variable);
}
property.serializedObject.ApplyModifiedProperties();
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
//This will need to be adjusted based on what you are displaying
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label);
}
}
[CustomPropertyDrawer(typeof(VariableReferenceBool))]
public class VariableReferenceBoolEditor : VariableReferenceEditor
{
public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
{
return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableBool), false);
}
}
[CustomPropertyDrawer(typeof(VariableReferenceFloat))]
public class VariableReferenceFloatEditor : VariableReferenceEditor
{
public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
{
return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableFloat), false);
}
}
[CustomPropertyDrawer(typeof(VariableReferenceString))]
public class VariableReferenceStringEditor : VariableReferenceEditor
{
public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
{
return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableString), false);
}
}
[CustomPropertyDrawer(typeof(VariableReferenceInt))]
public class VariableReferenceIntEditor : VariableReferenceEditor
{
public override UnityEngine.Object GetObjectField(Rect inRect, SerializedProperty inProperty)
{
return EditorGUI.ObjectField(inRect, "Variable", inProperty.objectReferenceValue, typeof(VariableInt), false);
}
}
and that’s the result i got
Thanks