Search Unity

Question Transform 2D array losing reference on 'Play'

Discussion in 'Immediate Mode GUI (IMGUI)' started by PedroBitencourt, Jan 16, 2022.

  1. PedroBitencourt

    PedroBitencourt

    Joined:
    Aug 20, 2019
    Posts:
    5
    This image is the Scriptable Object that I am trying to store a 2D array of Transforms, but whenever I press 'Play' or update the script/editor all the references get lost




    I am serializing my custom class on the ScriptableObject, but it just don't work :/
    2D Array really can't be serialized?


    Scriptable Object code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "Game Settings", menuName = "ScriptableObjects/Game Settings", order = 1)]
    4. public class GameSettings : ScriptableObject
    5. {
    6.     public Level level;
    7. }
    8.  
    9. [System.Serializable]
    10. public class Level
    11. {
    12.     public Transform[,] spawnPoints = new Transform[4, 4];
    13. }

    Editor code:

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(GameSettings))]
    7. public class GameSettingsEditor : Editor
    8. {
    9.     public override void OnInspectorGUI()
    10.     {
    11.         GameSettings settings = (GameSettings)target;
    12.         EditorGUILayout.Space();
    13.  
    14.         EditorGUI.indentLevel++;
    15.  
    16.  
    17.         EditorGUI.indentLevel = 0;
    18.  
    19.         GUIStyle tableStyle = new GUIStyle("box");
    20.         tableStyle.padding = new RectOffset(20, 20, 20, 20);
    21.         tableStyle.margin.left = 12;
    22.  
    23.         GUIStyle headerColumnStyle = new GUIStyle();
    24.         headerColumnStyle.fixedWidth = 65;
    25.  
    26.         GUIStyle columnStyle = new GUIStyle();
    27.         columnStyle.fixedWidth = 120;
    28.  
    29.         GUIStyle rowStyle = new GUIStyle();
    30.         rowStyle.fixedHeight = 70;
    31.  
    32.         GUIStyle rowHeaderStyle = new GUIStyle();
    33.         rowHeaderStyle.fixedWidth = columnStyle.fixedWidth - 1;
    34.  
    35.         GUIStyle columnHeaderStyle = new GUIStyle();
    36.         columnHeaderStyle.fixedWidth = 80;
    37.         columnHeaderStyle.fixedHeight = 70;
    38.  
    39.         GUIStyle columnLabelStyle = new GUIStyle();
    40.         columnLabelStyle.fixedWidth = rowHeaderStyle.fixedWidth - 6;
    41.         columnLabelStyle.alignment = TextAnchor.MiddleCenter;
    42.         columnLabelStyle.fontStyle = FontStyle.Bold;
    43.  
    44.         GUIStyle cornerLabelStyle = new GUIStyle();
    45.         cornerLabelStyle.fixedWidth = 42;
    46.         cornerLabelStyle.alignment = TextAnchor.MiddleRight;
    47.         cornerLabelStyle.fontStyle = FontStyle.BoldAndItalic;
    48.         cornerLabelStyle.fontSize = 14;
    49.         cornerLabelStyle.padding.top = -5;
    50.  
    51.         GUIStyle headerStyle = new GUIStyle();
    52.         headerStyle.fixedWidth = 80;
    53.         headerStyle.alignment = TextAnchor.MiddleCenter;
    54.         headerStyle.fontStyle = FontStyle.BoldAndItalic;
    55.         headerStyle.fontSize = 14;
    56.         headerStyle.padding.top = -40;
    57.         headerStyle.padding.left = 120;
    58.  
    59.         GUIStyle rowLabelStyle = new GUIStyle();
    60.         rowLabelStyle.fixedWidth = 25;
    61.         rowLabelStyle.alignment = TextAnchor.MiddleRight;
    62.         rowLabelStyle.fontStyle = FontStyle.Bold;
    63.         rowStyle.fixedWidth = 110;
    64.  
    65.         EditorGUILayout.BeginHorizontal(tableStyle);
    66.         for (int x = -1; x < 4; x++)
    67.         {
    68.             EditorGUILayout.BeginVertical((x == -1) ? headerColumnStyle : columnStyle);
    69.             for (int y = -1; y < 4; y++)
    70.             {
    71.                 if (x == -1 && y == -1)
    72.                 {
    73.                     EditorGUILayout.BeginVertical(rowHeaderStyle);
    74.                     EditorGUILayout.LabelField("[X,Y]  Note spawn positions", headerStyle);
    75.                     EditorGUILayout.EndHorizontal();
    76.                 }
    77.                 else if (x == -1)
    78.                 {
    79.                     EditorGUILayout.BeginVertical(columnHeaderStyle);
    80.                     EditorGUILayout.LabelField(y.ToString(), rowLabelStyle);
    81.                     EditorGUILayout.EndHorizontal();
    82.                 }
    83.                 else if (y == -1)
    84.                 {
    85.                     EditorGUILayout.BeginVertical(rowHeaderStyle);
    86.                     EditorGUILayout.LabelField(x.ToString(), columnLabelStyle);
    87.                     EditorGUILayout.EndHorizontal();
    88.                 }
    89.  
    90.                 if (x >= 0 && y >= 0)
    91.                 {
    92.                     EditorGUILayout.BeginHorizontal(rowStyle);
    93.              
    94.                     settings.level.spawnPoints[x, y] = (Transform)EditorGUILayout.ObjectField(settings.level.spawnPoints[x, y], typeof(Transform), true);
    95.                     EditorGUILayout.EndHorizontal();
    96.                 }
    97.             }
    98.             EditorGUILayout.EndVertical();
    99.         }
    100.         EditorGUILayout.EndHorizontal();
    101.  
    102.     }
    103. }
    104.  
    105.  
     
    Last edited: Jan 16, 2022