using System.Diagnostics.CodeAnalysis; namespace Editor; /// /// A control widget that converts its property into a SerializedObject, so it can edit subproperties /// public class ControlObjectWidget : ControlWidget { public SerializedObject SerializedObject { get; private set; } /// /// Do we want to create a new instance when editing a property of /// the given type containing null? /// private static bool ShouldCreateInstanceWhenNull( SerializedProperty property ) { var type = property.PropertyType; if ( type.IsAbstract ) return false; // Allow Nullable to be null if ( type.IsValueType ) return false; // There's nothing to edit inside a plain System.Object instance if ( type == typeof( object ) ) return false; if ( property.HasAttribute() ) return false; return true; } public ControlObjectWidget( SerializedProperty property, bool create ) : base( property ) { try { // // Create new entry, no null? // if ( create && property.GetValue() == null && ShouldCreateInstanceWhenNull( property ) ) { var newValue = Activator.CreateInstance( property.PropertyType ); property.SetValue( newValue ); } } catch ( Exception e ) { Log.Warning( $"Couldn't create ControlObjectWidget: {e}" ); } if ( property.TryGetAsObject( out var obj ) ) { SerializedObject = obj; } } }