Search Unity

Question Reorderable List is never selecter or focused

Discussion in 'Scripting' started by NamelessGames_, Aug 1, 2022.

  1. NamelessGames_

    NamelessGames_

    Joined:
    Jun 4, 2019
    Posts:
    43
    Hi all,

    I've a custom Editor where I use a Reorderable List. Its elements are never focused or selecter neither I use custom method neither default ones.

    Can you help me?

    Here is the code.

    Code (CSharp):
    1.  
    2.         ReorderableList _reorderableStartingElements;
    3.  
    4. public override void OnInspectorGUI()
    5.         {          
    6.             GUIStyle HeaderBackground = new GUIStyle("RL Header");
    7.  
    8.             Rect position = EditorGUILayout.GetControlRect(false, 7.5f);
    9.             position.height = EditorGUIUtility.singleLineHeight;
    10.  
    11.             _reorderableStartingElements = new ReorderableList(serializedObject, serializedObject.FindProperty("_startingElements"), true, true, true, true)
    12.            {
    13.                 drawElementBackgroundCallback = DrawListElementBackground,
    14.                 elementHeight = 19,
    15.                 elementHeightCallback = CalculateCallHeight
    16.             };
    17.  
    18.                 _reorderableStartingElements.DoLayoutList();
    19.  
    20.             serializedObject.ApplyModifiedProperties();
    21.         }
    22.        
    23. private float CalculateCallHeight(int index)
    24.         {
    25.             return EditorGUI.GetPropertyHeight(_reorderableStartingElements.serializedProperty.GetArrayElementAtIndex(index));
    26.         }
    27.  
    28.         public void DrawListElementBackground(Rect rect, int index, bool selected, bool focused)
    29.         {
    30.             if (Event.current.type != EventType.Repaint)
    31.                 return;
    32.  
    33.             GUIStyle HeaderBackground = new GUIStyle("RL Header");
    34.  
    35.             Color[] pix = new Color[Mathf.FloorToInt(rect.width * rect.height)];
    36.            
    37.             for (int i = 0; i < pix.Length; i++)
    38.                 pix[i] = selected || focused ? Color.blue : index % 2 == 0 ? Color.cyan : Color.yellow;
    39.            
    40.             Texture2D result = new Texture2D((int)rect.width, (int)5);
    41.             result.SetPixels(pix);
    42.             result.Apply();
    43.            
    44.             HeaderBackground.normal.background = result;
    45.             EditorGUI.LabelField(rect, "", HeaderBackground);
    46.  
    47.             //ReorderableList.defaultBehaviours.elementBackground.Draw(rect, false, selected, selected, focused);
    48.         }
    49.  
    "_startingElements" is a simple List<int>. I let the comment where I use the default ReorderableList Draw method withous succeed.

    Thank you in advance.

    P.s.
    Code is not optimized, I'm trying to make work it before.