diff --git a/engine/Sandbox.System/SerializedObject/CustomEditorAttribute.cs b/engine/Sandbox.System/SerializedObject/CustomEditorAttribute.cs index bccb961c0..8870c54f2 100644 --- a/engine/Sandbox.System/SerializedObject/CustomEditorAttribute.cs +++ b/engine/Sandbox.System/SerializedObject/CustomEditorAttribute.cs @@ -59,7 +59,7 @@ public float GetEditorScore( SerializedProperty property ) if ( TargetType is not null && !ForMethod ) { // - // Order by derived classes so we get the most relevent editor + // Order by derived classes so we get the most relevant editor // Type baseType = TargetType; while ( baseType.BaseType != null ) diff --git a/engine/Sandbox.Tools/Inspector/ComponentEditorWidget.cs b/engine/Sandbox.Tools/Inspector/ComponentEditorWidget.cs index 3e56ec04d..933731035 100644 --- a/engine/Sandbox.Tools/Inspector/ComponentEditorWidget.cs +++ b/engine/Sandbox.Tools/Inspector/ComponentEditorWidget.cs @@ -19,15 +19,36 @@ public ComponentEditorWidget( SerializedObject obj ) : base( null ) public static ComponentEditorWidget Create( SerializedObject obj ) { + var componentType = TypeLibrary.GetType( obj.TypeName ).TargetType; + var editor = EditorTypeLibrary.GetTypesWithAttribute( false ) .Where( x => x.Type.TargetType.IsAssignableTo( typeof( ComponentEditorWidget ) ) ) - .Where( x => x.Attribute.TargetType.Name == obj.TypeName ) + .Select( x => (score: GetEditorScore( x.Attribute.TargetType, componentType ), x.Type) ) + .Where( x => x.score > 0 ) + .OrderBy( x => x.score ) .FirstOrDefault(); if ( editor.Type == null ) return null; return editor.Type.Create( new object[] { obj } ); } + private static int GetEditorScore( Type targetType, Type componentType ) + { + int score = 0; + + // + // Order by derived classes so we get the most relevant editor + // + Type baseType = componentType; + while ( targetType.IsAssignableFrom( baseType ) ) + { + score++; + baseType = baseType.BaseType; + } + + return score; + } + public virtual void OnHeaderContextMenu( Menu menu ) {