Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Custom property drawer made the scrollbar disappear

Discussion in 'Extensions & OnGUI' started by Rogatien, Aug 18, 2017.

  1. Rogatien

    Rogatien

    Joined:
    Jun 2, 2017
    Posts:
    1
    Hi all!

    I couldn't find another thread with the same problem so I figured I would post here.

    I wrote a custom property drawer for a Dialogue class. The property is displayed in a scriptableObject called DialogueTree. It is basically a list of nested instances of the Dialogue class.

    I finally managed to make everything look good enough, but in the process my scrollbar has disappeared, making the inspector useless.

    Anyone has a clue why? Here's the code for the PropertyDrawer (it's a bit messy) :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomPropertyDrawer(typeof(Dialogue))]
    7. public class DialogueDrawer : PropertyDrawer {
    8.  
    9.     string[] selectionEnumNames = new string[]{"Show dialogue", "Back to entry point", "Back one step", "End"};
    10.     bool isRoot;
    11.  
    12.     Rect nameRect;
    13.     Rect nameLabelRect;
    14.  
    15.     Rect selectionRect;
    16.     Rect selectionLabelRect;
    17.  
    18.     Rect textRect;
    19.     Rect textLabelRect;
    20.  
    21.     Rect answersNumberRect;
    22.     Rect answersNumberLabelRect;
    23.  
    24.     Rect answersRect;
    25.  
    26.     //Format variables
    27.  
    28.     float line = 17;
    29.     float largeLine = 60;
    30.     float smallIndent = 75;
    31.     float indent = 150;
    32.     float largeIndent = 300;
    33.     float dialogueHeight = 145;
    34.  
    35.  
    36.     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
    37.     {
    38.         //dialogueHeight = line*4 + largeLine;
    39.  
    40.         isRoot = property.FindPropertyRelative("isRoot").boolValue;
    41.  
    42.         EditorGUI.BeginProperty(position, label, property);
    43.  
    44.         property.isExpanded = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, line), property.isExpanded, label);
    45.  
    46.         position.y += line;
    47.  
    48.         if (property.isExpanded)
    49.         {
    50.  
    51.             if (isRoot)
    52.             {
    53.                 textRect = new Rect(position.x + indent, position.y, largeIndent, largeLine);
    54.                 textLabelRect = new Rect(position.x, position.y, position.width, line);
    55.                 position.y += largeLine;
    56.  
    57.                 EditorGUI.PrefixLabel(textLabelRect, new GUIContent("Text to show"));
    58.                 EditorGUI.PropertyField(textRect, property.FindPropertyRelative("text"), GUIContent.none);
    59.             }
    60.  
    61.             if (!isRoot)
    62.             {
    63.                 nameRect = new Rect(position.x + indent, position.y, largeIndent, line);
    64.                 nameLabelRect = new Rect(position.x, position.y, indent, line);
    65.                 position.y += line;
    66.  
    67.                 EditorGUI.PrefixLabel(nameLabelRect, new GUIContent("Answer"));
    68.                 EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);
    69.  
    70.                 selectionRect = new Rect(position.x + indent, position.y, indent, line);
    71.                 selectionLabelRect = new Rect(position.x, position.y, indent, line);
    72.                 position.y += line;
    73.  
    74.                 EditorGUI.PrefixLabel(selectionLabelRect, new GUIContent("Effect"));
    75.                 property.FindPropertyRelative("selectionResults").intValue = EditorGUI.Popup(selectionRect, property.FindPropertyRelative("selectionResults").intValue, selectionEnumNames);
    76.  
    77.                 textRect = new Rect(position.x + indent, position.y, largeIndent, largeLine);
    78.                 textLabelRect = new Rect(position.x, position.y, position.width, line);
    79.                 position.y += largeLine;
    80.  
    81.                 EditorGUI.PrefixLabel(textLabelRect, new GUIContent("Text to show"));
    82.                 EditorGUI.PropertyField(textRect, property.FindPropertyRelative("text"), GUIContent.none);
    83.             }
    84.  
    85.             answersNumberRect = new Rect(position.x+indent, position.y, indent/2, line);
    86.             answersNumberLabelRect = new Rect(position.x, position.y, position.width, line);
    87.             position.y += line;
    88.  
    89.             int answersLength = EditorGUI.IntPopup(answersNumberRect, property.FindPropertyRelative("answers").arraySize, new string[] {"0", "1", "2", "3", "4", "5"}, new int[] {0,1,2,3,4,5});
    90.             EditorGUI.PrefixLabel(answersNumberLabelRect, new GUIContent("Number of answers"));
    91.                
    92.             answersRect = new Rect(position.x + smallIndent, position.y, largeIndent, line);
    93.             position.y += line;
    94.  
    95.             for (int i = 0; i < property.FindPropertyRelative("answers").arraySize; i++) {
    96.                
    97.                 string name = property.FindPropertyRelative("answers").GetArrayElementAtIndex(i).FindPropertyRelative("name").stringValue;
    98.                 if (name == "")
    99.                 {
    100.                     name = "Unnamed element";
    101.                 }
    102.  
    103.                 if (property.FindPropertyRelative("answers").GetArrayElementAtIndex(i).isExpanded){
    104.                    
    105.                     EditorGUI.PropertyField(new Rect(answersRect.x, position.y, answersRect.width, line),
    106.                         property.FindPropertyRelative("answers").GetArrayElementAtIndex(i), new GUIContent(name));
    107.                     position.y += GetPropertyHeight(property.FindPropertyRelative("answers").GetArrayElementAtIndex(i));
    108.                 } else
    109.                 {
    110.                     EditorGUI.PropertyField(new Rect(answersRect.x, position.y, answersRect.width, line), property.FindPropertyRelative("answers").GetArrayElementAtIndex(i), new GUIContent(name));
    111.                     position.y += line;
    112.                 }
    113.             }
    114.  
    115.  
    116.             EditorGUI.EndProperty();
    117.  
    118.             while (answersLength < property.FindPropertyRelative("answers").arraySize)
    119.             {
    120.                 property.FindPropertyRelative("answers").DeleteArrayElementAtIndex(property.FindPropertyRelative("answers").arraySize-1);
    121.             }
    122.  
    123.             while (answersLength > property.FindPropertyRelative("answers").arraySize)
    124.             {
    125.                 property.FindPropertyRelative("answers").InsertArrayElementAtIndex(property.FindPropertyRelative("answers").arraySize);
    126.             }
    127.         }
    128.  
    129.     }
    130.  
    131.     public float GetPropertyHeight (SerializedProperty property)
    132.     {
    133.         float height = dialogueHeight;
    134.         Queue<SerializedProperty> answers = new Queue<SerializedProperty>();
    135.  
    136.         if (!property.FindPropertyRelative("answers").isExpanded)
    137.             return height;
    138.            
    139.  
    140.         for (int i = 0; i < property.FindPropertyRelative("answers").arraySize; i++) {
    141.             answers.Enqueue(property.FindPropertyRelative("answers").GetArrayElementAtIndex(i));
    142.         }
    143.  
    144.         while (answers.Count > 0)
    145.         {
    146.  
    147.             if (!answers.Peek().isExpanded)
    148.             {
    149.                 height += line;
    150.                 answers.Dequeue();
    151.  
    152.             }
    153.  
    154.             else
    155.             {
    156.                 for (int i = 0; i < answers.Peek().FindPropertyRelative("answers").arraySize; i++) {
    157.                     answers.Enqueue(answers.Peek().FindPropertyRelative("answers").GetArrayElementAtIndex(i));
    158.                 }
    159.                    
    160.                 height += dialogueHeight;
    161.  
    162.                 answers.Dequeue();
    163.             }
    164.         }
    165.  
    166.         return (height);
    167.     }
    168.  
    169. }
    170.  
    And here's a picture of the problem:
    no scrollbar.PNG
     
  2. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    389
    You havn't told it you want one :p Take a look at EditorGUILayout.BeginScrollView.

    I am not 100% confident that is what its called, I am more used to using EditorGUILayout.BeginScrollView but there is usually an equivalent in EditorGui
     
    Last edited: Aug 29, 2018
  3. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    31
    Hello,
    Same issue here.
    And @LaireonGames unfortunately, EditorGUI.BeginScrollView doesn't exist :(
    @Rogatien I was thinking maybe it's because you execute the EditorGUI.EndProperty in your condition "if (property.isExpanded)", so the EndProperty is not always called?
     
    Last edited: Aug 29, 2018
  4. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    389
    Ah sorry its:

    EditorGUILayout.BeginScrollView

    Edit: Oh sorry re-read this too quick! Yeah the one I linked wont be applicable sadly but in general if you want a scroll view you gotta set one up
     
  5. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    31
    Okay, in my case the problem is solved when I override GetPropertyHeight.
    At the moment I just wrote a dirty hack:

    Code (CSharp):
    1. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    2. {
    3.     return 140f;
    4. }
    Of course, it's dirty because it doesn't take care of the fields.
    A day will come when the amount of fields in my property will change and everything will be broken.
    But for now, my scrollbar is back! Yay :)