Search Unity

CustomPropertyDrawer, 2D Array, Limits Y Length of Array?

Discussion in 'Scripting' started by renman3000, Nov 13, 2018.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi there,
    I am using a CustomPropertyDrawer to create a 2D Array that will display in my editor, inspector. My issue is that, for some reason, the second length of the array, so array[x,y], if y > 8, I get an error, of out of index range.

    I am unsure why.

    I can set the x comfortably, and as long as the y is < 8, I can create any 2D array for my inspector, but over that, it clips out.

    Why is this?


    Code (csharp):
    1.  
    2. //editor script.
    3.  
    4.  
    5. using System.Collections;
    6. using UnityEditor;
    7. using UnityEngine;
    8.  
    9.  
    10.  
    11.  
    12.  
    13. [CustomPropertyDrawer(typeof(EditorLevelLayout))]
    14. public class GUILevelLayout : PropertyDrawer
    15. {
    16.  
    17.  
    18.  
    19.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label){
    20.  
    21.  
    22.  
    23.         EditorGUI.PrefixLabel(position, label);
    24.  
    25.  
    26.         Rect newPosition = position;
    27.         newPosition.y += 20f;
    28.         SerializedProperty row = property.FindPropertyRelative("row");
    29.  
    30.    
    31.         int editorHeight = 12;
    32.         int editorWidth = 11;
    33.  
    34.  
    35.         for (int i = 0; i < editorHeight; i++){
    36.             Debug.Log(i + " i " + editorHeight);
    37.  
    38.  
    39.             SerializedProperty column = row.GetArrayElementAtIndex(i).FindPropertyRelative("column");
    40.  
    41.             newPosition.height = 20;
    42.             newPosition.width = 20;
    43.  
    44.  
    45.             if(column.arraySize != editorWidth){
    46.                 column.arraySize = editorWidth;
    47.             }
    48.  
    49.  
    50.             for (int m = 0; m < editorWidth; m++){
    51.                 Debug.Log(m + " m " + editorWidth);
    52.                 EditorGUI.PropertyField(newPosition, column.GetArrayElementAtIndex(m), GUIContent.none);
    53.                 newPosition.x += newPosition.width;
    54.  
    55.             }
    56.  
    57.  
    58.             newPosition.x = position.x;
    59.             newPosition.y += 20;
    60.  
    61.  
    62.         }
    63.  
    64.     }
    65.  
    66.  
    67.  
    68.  
    69.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    70.     {
    71.         return base.GetPropertyHeight(property, label);
    72.     }
    73.  
    74.  
    75.  
    76.  
    77.  
    78. }
    79.  
    80.  

    Code (csharp):
    1.  
    2.  
    3.  
    4. using System.Collections;
    5. using UnityEngine;
    6.  
    7. [System.Serializable]
    8. public class EditorLevelLayout{
    9.  
    10.  
    11.  
    12.     [System.Serializable]
    13.     public struct rowData{
    14.  
    15.         public int[] column;
    16.  
    17.     }
    18.  
    19.     public rowData[] row = new rowData[12];
    20.  
    21.  
    22. }
    23.