I am trying to make a pokemon game. I define a 2d float array and set its size to 18x18. I use a custom editor to fill the chart in inspector. It works fine in editor but all values reset to zero when i start the game.
- Unity can not serialize multi-dimensional arrays. Normally, Unity inspector displays serialized fields, but you made your own editor GUI. That still doesn’t make the array serializable. The easiest workaround would be to have a single dimension array, and simply map the 2D coordinates to it. So instead of having
new float[18,18]
, you’dnew float[18*18]
… - Even if Unity did serialize multi-dimensional arrays, your custom editor sets the array values directly, meaning that a lot of editor features won’t work, such as Undo, or automatically recognizing that the object was changed and needs to be saved with the scene. The latter can be fixed by calling EditorUtility.SetDirty after you have modified the object directly. By directly I mean by not using the SerializedObject / SerializedProperty provided for you by the Editor class.
To make it work, you have to fix BOTH points.
using UnityEngine;
public class TypeManager : MonoBehaviour
{
public float[,] Chart = new float[18,18];
}
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TypeManager))]
public class TypeEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUIUtility.fieldWidth = 5;
EditorGUIUtility.labelWidth = 5;
TypeManager types = (TypeManager)target;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space(16);
EditorGUILayout.LabelField("Normal");
EditorGUILayout.LabelField("Fire");
EditorGUILayout.LabelField("Water");
EditorGUILayout.LabelField("Grass");
EditorGUILayout.LabelField("Electric");
EditorGUILayout.LabelField("Ice");
EditorGUILayout.LabelField("Fighting");
EditorGUILayout.LabelField("Poison");
EditorGUILayout.LabelField("Ground");
EditorGUILayout.LabelField("Flying");
EditorGUILayout.LabelField("Psychic");
EditorGUILayout.LabelField("Bug");
EditorGUILayout.LabelField("Rock");
EditorGUILayout.LabelField("Ghost");
EditorGUILayout.LabelField("Dragon");
EditorGUILayout.LabelField("Dark");
EditorGUILayout.LabelField("Steel");
EditorGUILayout.LabelField("Fairy");
EditorGUILayout.EndHorizontal();
for(int i=0; i<18; i++)
{
GUILayout.BeginHorizontal();
switch (i)
{
case 0:
EditorGUILayout.LabelField("Normal");
break;
case 1:
EditorGUILayout.LabelField("Fire");
break;
case 2:
EditorGUILayout.LabelField("Water");
break;
case 3:
EditorGUILayout.LabelField("Grass");
break;
case 4:
EditorGUILayout.LabelField("Electric");
break;
case 5:
EditorGUILayout.LabelField("Ice");
break;
case 6:
EditorGUILayout.LabelField("Fighting");
break;
case 7:
EditorGUILayout.LabelField("Posion");
break;
case 8:
EditorGUILayout.LabelField("Ground");
break;
case 9:
EditorGUILayout.LabelField("Flying");
break;
case 10:
EditorGUILayout.LabelField("Psychic");
break;
case 11:
EditorGUILayout.LabelField("Bug");
break;
case 12:
EditorGUILayout.LabelField("Rock");
break;
case 13:
EditorGUILayout.LabelField("Ghost");
break;
case 14:
EditorGUILayout.LabelField("Dragon");
break;
case 15:
EditorGUILayout.LabelField("Dark");
break;
case 16:
EditorGUILayout.LabelField("Steel");
break;
case 17:
EditorGUILayout.LabelField("Fairy");
break;
}
for (int j=0; j<18; j++)
{
types.Chart[i, j] = EditorGUILayout.FloatField(types.Chart[i,j]);
}
GUILayout.EndHorizontal();
}
}
}