Skip to content

Instantly share code, notes, and snippets.

@tomkail
Last active April 15, 2024 14:07
Show Gist options
  • Save tomkail/ba8d49e1cee021b0b89d47fca68b53a2 to your computer and use it in GitHub Desktop.
Save tomkail/ba8d49e1cee021b0b89d47fca68b53a2 to your computer and use it in GitHub Desktop.
Unity editor helper that draws a serialized property (including children) fully, even if it's an instance of a custom serializable class.
using UnityEngine;
using UnityEditor;
using System.Collections;
public static class BetterPropertyField {
/// <summary>
/// Draws a serialized property (including children) fully, even if it's an instance of a custom serializable class.
/// Supersedes EditorGUILayout.PropertyField(serializedProperty, true);
/// </summary>
/// <param name="_serializedProperty">Serialized property.</param>
public static void DrawSerializedProperty (SerializedProperty _serializedProperty) {
if(_serializedProperty == null) {
EditorGUILayout.HelpBox("SerializedProperty was null!", MessageType.Error);
return;
}
var serializedProperty = _serializedProperty.Copy();
int startingDepth = serializedProperty.depth;
EditorGUI.indentLevel = serializedProperty.depth;
DrawPropertyField(serializedProperty);
while (serializedProperty.NextVisible(serializedProperty.isExpanded && !PropertyTypeHasDefaultCustomDrawer(serializedProperty.propertyType)) && serializedProperty.depth > startingDepth) {
EditorGUI.indentLevel = serializedProperty.depth;
DrawPropertyField(serializedProperty);
}
}
static void DrawPropertyField (SerializedProperty serializedProperty) {
if(serializedProperty.propertyType == SerializedPropertyType.Generic) {
serializedProperty.isExpanded = EditorGUILayout.Foldout(serializedProperty.isExpanded, serializedProperty.displayName, true);
} else {
EditorGUILayout.PropertyField(serializedProperty);
}
}
static bool PropertyTypeHasDefaultCustomDrawer(SerializedPropertyType type) {
return
type == SerializedPropertyType.AnimationCurve ||
type == SerializedPropertyType.Bounds ||
type == SerializedPropertyType.Color ||
type == SerializedPropertyType.Gradient ||
type == SerializedPropertyType.LayerMask ||
type == SerializedPropertyType.ObjectReference ||
type == SerializedPropertyType.Rect ||
type == SerializedPropertyType.Vector2 ||
type == SerializedPropertyType.Vector3;
}
}
@restush
Copy link

restush commented May 9, 2023

I'm not sure, but using this on editor window, the remove and add button of array is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment