Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SerializeReference] GenericSerializedReferenceInspectorUI

Discussion in '2020.1 Beta' started by TextusGames, Jan 21, 2020.

?

Do you want to have the ability to assign polymorphic classes right in inspector with default UI?

  1. Yes.

    396 vote(s)
    82.2%
  2. No.

    5 vote(s)
    1.0%
  3. Yes. You implementation is good.

    4 vote(s)
    0.8%
  4. Yes. Unity should adapt this implementation

    47 vote(s)
    9.8%
  5. Yes. Unity should make some different Ui

    30 vote(s)
    6.2%
  1. aofekiko

    aofekiko

    Joined:
    Dec 20, 2014
    Posts:
    1
    Incredible and so simple to use, you've made my work so much easier.
    My greatest of thanks!
     
    TextusGames likes this.
  2. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    135
    Your typename split method ManagedReferenceUtility.GetSplitNamesFromTypename doesn't handle assembly names that have spaces.

    You might want to adapt it like this :

    Code (CSharp):
    1.  
    2.    /// Get assembly and class names from typeName
    3.     public static (string AssemblyName, string ClassName) GetSplitNamesFromTypename(string typename)
    4.     {
    5.         if (string.IsNullOrEmpty(typename))
    6.             return ("","");
    7.      
    8.         var lastSpaceIndex = typename.LastIndexOf ( ' ' );
    9.         if ( lastSpaceIndex == -1 )
    10.             return ("", "");
    11.      
    12.         var assemblyName = typename.Substring ( 0, lastSpaceIndex );
    13.         var className = typename.Substring ( lastSpaceIndex + 1 );
    14.  
    15.         return ( assemblyName, className );
    16.     }
    17.  
     
    TextusGames likes this.
  3. thebestpronagibator

    thebestpronagibator

    Joined:
    Mar 12, 2019
    Posts:
    2
    If you have "overlapping" issue in inspector while adding new instance (changing type) like me:
    upload_2022-10-9_3-48-46.png
    so just add extra code lines to fix that (property.isExpanded = false):

    Code (CSharp):
    1.     public static void DrawSelectionButtonForManagedReference(this SerializedProperty property,
    2.         Rect position, Color color, IEnumerable<Func<Type, bool>> filters = null)
    3.     {
    4.  
    5.         var backgroundColor = color;
    6.  
    7.         var buttonPosition = position;
    8.         buttonPosition.x += EditorGUIUtility.labelWidth + 1 * EditorGUIUtility.standardVerticalSpacing;
    9.         buttonPosition.width = position.width - EditorGUIUtility.labelWidth - 1 * EditorGUIUtility.standardVerticalSpacing;
    10.         buttonPosition.height = EditorGUIUtility.singleLineHeight;
    11.  
    12.         var storedIndent = EditorGUI.indentLevel;
    13.         EditorGUI.indentLevel = 0;
    14.         var storedColor = GUI.backgroundColor;
    15.         GUI.backgroundColor = backgroundColor;
    16.  
    17.  
    18.         var names = ManagedReferenceUtility.GetSplitNamesFromTypename(property.managedReferenceFullTypename);
    19.         var className = string.IsNullOrEmpty(names.ClassName) ? "Null (Assign)" : names.SimpleName;
    20.         if (GUI.Button(buttonPosition, new GUIContent(className, className)))
    21.         {
    22.             property.ShowContextMenuForManagedReference(filters);
    23.             property.isExpanded = false;
    24.         }
    25.  
    26.         GUI.backgroundColor = storedColor;
    27.         EditorGUI.indentLevel = storedIndent;
    28.     }
    Thats unexpend your property but fix the issue