Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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.

    311 vote(s)
    80.2%
  2. No.

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

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

    43 vote(s)
    11.1%
  5. Yes. Unity should make some different Ui

    27 vote(s)
    7.0%
  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:
    118
    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