I am working on my personal toy project and custom editor is causing me some trouble.
I realized that all variables must be serializable to store values even after the Unity client is terminated.
Sofar it works fine with enum, int and array but for some reason two dimentional array is keep losing it's variables. I wonder why this happens on multi dimentional arrays.
here's a code that I am trying to understand.
StaticBlockData.cs (ScriptableObject script)
using UnityEngine;
namespace Scriptable
{
[System.Serializable]
[CreateAssetMenu(fileName = "Block", menuName = "Scriptable/Data")]
public class StaticBlockData : ScriptableObject
{
public BlockValue e_blockValue = 0;
public int i_blockCount = 0;
public bool[] b_slots = new bool[9];
public bool[,] b_slots2 = new bool[3,3];
}
}
BlockShapeCustomEditor.cs (Custom Editor script)
using Scriptable;
using UnityEditor;
using UnityEngine;
namespace FACustomEditor
{
// Custom Editor targets "StaticBlockData" script
[CustomEditor(typeof(StaticBlockData)), CanEditMultipleObjects]
// Inheritant UnityEditor.Editor script
public class BlockShapeCustomEditor : Editor
{
/// <summary>
/// Drawing GUI on inspector mode
/// </summary>
public override void OnInspectorGUI()
{
// Define current target script data
StaticBlockData blockData = (StaticBlockData)target;
// Create enum popup inspector GUI
blockData.e_blockValue = (BlockValue)EditorGUILayout.EnumPopup("Block Value", blockData.e_blockValue);
// Create int slider inspector GUI
blockData.i_blockCount = EditorGUILayout.IntSlider("Block Count", blockData.i_blockCount, 1, 9);
// Create a label for the boolean field
EditorGUILayout.LabelField("Block Shape");
// Start a horizontal layout group
EditorGUILayout.BeginHorizontal();
// Create a boolean field
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
// Create toggle inspector GUI with maximum width size (By setting maximum width size GUI can trim the space between each elements)
blockData.b_slots[x + (y * 3)] = EditorGUILayout.Toggle(blockData.b_slots[x + (y * 3)], GUILayout.MaxWidth(20));
}
// Change line after every 3 toggle creation
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginVertical();
EditorGUILayout.EndVertical();
EditorGUILayout.BeginHorizontal();
}
EditorGUILayout.EndHorizontal();
// Create a label for the boolean field
EditorGUILayout.LabelField("Block Shape2");
// Start a horizontal layout group
EditorGUILayout.BeginHorizontal();
// Create a boolean field
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
// Create toggle inspector GUI with maximum width size (By setting maximum width size GUI can trim the space between each elements)
blockData.b_slots2[x, y] = EditorGUILayout.Toggle(blockData.b_slots2[x, y], GUILayout.MaxWidth(20));
}
// Change line after every 3 toggle creation
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginVertical();
EditorGUILayout.EndVertical();
EditorGUILayout.BeginHorizontal();
}
EditorGUILayout.EndHorizontal();
// Send true signal when changes happen on GUI
if (GUI.changed)
{
// Dirty save when every the elements in "StaticBlockData" has changed
// Save the change progress in driver
EditorUtility.SetDirty(blockData);
}
}
}
}
I've tried change it to private and serialized it. I've tried to create integer two dimentional array. Same thing happen.
