Last active
April 15, 2024 14:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure, but using this on editor window, the remove and add button of array is gone.