Hi all, this has been bugging me for a little while now, and can’t seem to figure out a decent solution.
I’m writing a custom property drawer for an action class, which contains several foldouts. I’ve set it up so that each block of the foldout uses a float property for height that checks whether the bool for the foldout is toggled, and calculates it into the overall property height and positions of the inspector fields. This works fine when I have a single instance of the action class, but causes problems when they’re put into a list, where hitting the toggle activates the same foldout in each element in the list.
Is this a problem with using Lists with Property Drawers? Or is it my use of properties outside the OnGUI method? Or something else entirely? And is there a way to resolve it?
Thanks so much for any help or insight you might offer.
Here’s the code in question below:
[CustomPropertyDrawer(typeof(CoreCharacterAction))]
public class CoreCharacterActionDrawer : PropertyDrawer
{
bool
showContent = false,
showCosts = false,
showDuration = false,
showMovementSpeed = false,
showTurningSpeed = false;
float CostsHeight {
get {
if (showCosts)
return 45;
else
return 15;
}
}
float DurationsHeight {
get {
if (showDuration)
return 75;
else
return 15;
}
}
float MovementsHeight {
get {
if (showMovementSpeed)
return 75;
else
return 15;
}
}
float TurningHeight {
get {
if (showTurningSpeed)
return 75;
else
return 15;
}
}
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
float tempHeight = 15;
if (showContent) {
tempHeight += CostsHeight + DurationsHeight + MovementsHeight + TurningHeight;
}
Debug.LogWarning (tempHeight);
return tempHeight;
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUIUtility.LookLikeControls();
SerializedProperty
name = property.FindPropertyRelative ("name"),
stateEffect = property.FindPropertyRelative ("stateEffect"),
primaryVitalType = property.FindPropertyRelative ("primaryVitalType"),
secondaryVitalType = property.FindPropertyRelative ("secondaryVitalType"),
primaryCost = property.FindPropertyRelative ("primaryCost"),
secondaryCost = property.FindPropertyRelative ("secondaryCost"),
activationType = property.FindPropertyRelative ("activationType"),
enterDuration = property.FindPropertyRelative ("enterDuration"),
activeDuration = property.FindPropertyRelative ("activeDuration"),
activeInputName = property.FindPropertyRelative ("activeInputName"),
exitDuration = property.FindPropertyRelative ("exitDuration"),
moveType = property.FindPropertyRelative ("moveType"),
enterMoveSpeed = property.FindPropertyRelative ("enterMoveSpeed"),
activeMoveSpeed = property.FindPropertyRelative ("activeMoveSpeed"),
exitMoveSpeed = property.FindPropertyRelative ("exitMoveSpeed"),
lookType = property.FindPropertyRelative ("lookType"),
enterLookSpeed = property.FindPropertyRelative ("enterTurnSpeed"),
activeLookSpeed = property.FindPropertyRelative ("activeTurnSpeed"),
exitLookSpeed = property.FindPropertyRelative ("exitTurnSpeed");
showContent = EditorGUI.Foldout (new Rect (position.x,
position.y,
position.width,
15),
showContent, name.stringValue);
if (showContent) {
name.stringValue = EditorGUI.LabelField (EditorGUI.IndentedRect (new Rect (position.x,
position.y + 15,
position.width,
15)), name.stringValue);
showCosts = EditorGUI.Foldout (EditorGUI.IndentedRect(new Rect (position.x,
position.y+30,
position.width,
15)),
showCosts, "Resource Costs");
if (showCosts == true) {
EditorGUI.indentLevel = 2;
primaryVitalType.enumValueIndex = EditorGUI.Popup (EditorGUI.IndentedRect(new Rect (position.x,
position.y + 30,
position.width / 3,
15)),
primaryVitalType.enumValueIndex, primaryVitalType.enumNames);
if (primaryVitalType.enumValueIndex != 0)
primaryCost.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x + position.width / 3,
position.y + 30,
position.width * 2 / 3,
15)),
primaryCost.floatValue, 0, 50);
secondaryVitalType.enumValueIndex = EditorGUI.Popup (EditorGUI.IndentedRect(new Rect (position.x,
position.y + 45,
position.width / 3,
15)),
secondaryVitalType.enumValueIndex, secondaryVitalType.enumNames);
if (secondaryVitalType.enumValueIndex != 0)
secondaryCost.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x + position.width / 3,
position.y + 45,
position.width * 2 / 3,
15)),
secondaryCost.floatValue, 0, 50);
EditorGUI.indentLevel = 1;
}
showDuration = EditorGUI.Foldout (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + 15,
position.width / 3,
15)),
showDuration, "Effect Durations");
if (showDuration == true) {
EditorGUI.indentLevel = 2;
activationType.enumValueIndex = EditorGUI.Popup (EditorGUI.IndentedRect (new Rect (position.x,
position.y + CostsHeight + 30,
position.width,
15)),
activationType.enumValueIndex, activationType.enumNames);
enterDuration.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + 45,
position.width,
15)),
"startup duration", enterDuration.floatValue, 0, 5);
if (activationType.enumValueIndex == 0)
activeDuration.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + 60,
position.width,
15)),
"active duration", activeDuration.floatValue, 0, 5);
else
activeInputName.enumValueIndex = EditorGUI.Popup (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + 60,
position.width,
15)),
"active input", activeInputName.enumValueIndex, activeInputName.enumNames);
exitDuration.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + 75,
position.width,
15)),
"cooldown duration", exitDuration.floatValue, 0, 5);
EditorGUI.indentLevel = 1;
}
showMovementSpeed = EditorGUI.Foldout (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + DurationsHeight + 15,
position.width,
15)),
showMovementSpeed, "Movement Speeds");
if (showMovementSpeed) {
EditorGUI.indentLevel = 2;
moveType.enumValueIndex = EditorGUI.Popup (EditorGUI.IndentedRect (new Rect (position.x,
position.y + CostsHeight + DurationsHeight + 30,
position.width,
15)),
moveType.enumValueIndex, moveType.enumNames);
enterMoveSpeed.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect (new Rect (position.x,
position.y + CostsHeight + DurationsHeight + 45,
position.width,
15)),
enterMoveSpeed.floatValue, 0, 25);
activeMoveSpeed.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect (new Rect (position.x,
position.y + CostsHeight + DurationsHeight + 60,
position.width,
15)),
activeMoveSpeed.floatValue, 0, 25);
exitMoveSpeed.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect (new Rect (position.x,
position.y + CostsHeight + DurationsHeight + 75,
position.width,
15)),
exitMoveSpeed.floatValue, 0, 25);
EditorGUI.indentLevel = 1;
}
showTurningSpeed = EditorGUI.Foldout (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + DurationsHeight + MovementsHeight + 15,
position.width,
15)),
showTurningSpeed, "Turn Speeds");
if (showTurningSpeed) {
EditorGUI.indentLevel = 2;
lookType.enumValueIndex = EditorGUI.Popup (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + DurationsHeight + MovementsHeight + 30,
position.width,
15)),
lookType.enumValueIndex, lookType.enumNames);
enterLookSpeed.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + DurationsHeight + MovementsHeight + 45,
position.width,
15)),
enterLookSpeed.floatValue, 0, 25);
activeLookSpeed.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + DurationsHeight + MovementsHeight + 60,
position.width,
15)),
activeLookSpeed.floatValue, 0, 25);
exitLookSpeed.floatValue = EditorGUI.Slider (EditorGUI.IndentedRect(new Rect (position.x,
position.y + CostsHeight + DurationsHeight + MovementsHeight + 75,
position.width,
15)),
exitLookSpeed.floatValue, 0, 25);
EditorGUI.indentLevel = 1;
}
}
}
}