I'm writing a custom editor for my GridModel
object. The object has 2 fields; Vector2Int dimensions
and a Dictionary<Vector2Int, bool> grid
variable. Since the script will mostly be editted in the prefab editor, I save the fields with the serializedObject
property. This works fine for the dimensions
variable, but the serializedObject.FindProperty("grid")
method returns null
. My guess is this is because the Dictionary
object is not serializable.
My question therefor is, how can I save the Dictionary
type in the serializedObject
property to make sure the grid
field is the same in the prefab editor as in any other editor?
These are my scripts:
public class GridModel : MonoBehaviour, IGridModel
{
public Dictionary<Vector2Int, bool> grid;
public Vector2Int dimensions;
}
CustomEditor(typeof(GridModel))]
public class GridModelEditor : Editor
{
public override void OnInspectorGUI()
{
Vector2Int dimensions = EditorGUILayout.Vector2IntField("Size", serializedObject.FindProperty("dimensions").vector2IntValue);
serializedObject.FindProperty("dimensions").vector2IntValue = dimensions;
SerializedProperty serializedField = serializedObject.FindProperty("grid"); // Returns null
serializedObject.ApplyModifiedProperties();
}
}