- Home /
Custom Inspector for Scriptable Object with multi dimensional array
Hi Everyone, I'm trying to make custom inspector for my scriptable object, to group same indexes of different arrays together:
Following a Unity Tutorial, I was able to get to the point where I have them in group. But I've encountered 2 issues:
#1: I've hard coded number 4 in the Editor script. Can't figure out how to expand amount of Item slots in arrays based on howManyItems variable.
#2: I have array of class which contains two additional arrays. In default inspector this is expandable (screen1), but with custom arrow doesn't show what is under it (screen2).
Below my code: Scriptable Object, (I am creating scriptable object from added Assets menu):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "CraftingMachine", menuName = "FFYourLife/Crafting/CraftingMachine", order = 4)]
public class CraftingMachine : ScriptableObject {
public int howManyItems;
public string machineType;
public Item[] possibleObjectsToCreate;
public int[] levelOfMachineToCreateThisObject;
public MaterialsArray[] neededMaterials;
}
[System.Serializable]
public class MaterialsArray {
public Item[] groupOfMaterials;
public int[] quantityPerItemInGroup;
}
Custom Editor code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(CraftingMachine))]
[CanEditMultipleObjects]
public class CraftingMachineEditor : Editor {
private SerializedProperty howManyItemsProperty;
private SerializedProperty machineTypeProperty;
private SerializedProperty possibleObjectsToCreateProperty;
private SerializedProperty levelOfMachineToCreateThisObjectProperty;
private SerializedProperty neededMaterialsProperty;
private bool[] showMaterialSlots = new bool[4];
private const string craftingMachinePropHowManyItemsName = "howManyItems";
private const string craftingMachinePropMachineTypeName = "machineType";
private const string craftingMachinePropPossibleObjectsToCreateName = "possibleObjectsToCreate";
private const string craftingMachinePropLevelOfMachineToCreateThisObjectName = "levelOfMachineToCreateThisObject";
private const string craftingMachinePropNeededMaterialsName = "neededMaterials";
private void OnEnable() {
howManyItemsProperty = serializedObject.FindProperty(craftingMachinePropHowManyItemsName);
machineTypeProperty = serializedObject.FindProperty(craftingMachinePropMachineTypeName);
possibleObjectsToCreateProperty = serializedObject.FindProperty(craftingMachinePropPossibleObjectsToCreateName);
levelOfMachineToCreateThisObjectProperty = serializedObject.FindProperty(craftingMachinePropLevelOfMachineToCreateThisObjectName);
neededMaterialsProperty = serializedObject.FindProperty(craftingMachinePropNeededMaterialsName);
}
public override void OnInspectorGUI() {
serializedObject.Update();
EditorGUILayout.PropertyField(howManyItemsProperty);
EditorGUILayout.PropertyField(machineTypeProperty);
for (int i = 0; i < 4; i++) {
NewMaterialSlotGUI(i);
}
serializedObject.ApplyModifiedProperties();
}
private void NewMaterialSlotGUI(int index) {
EditorGUILayout.BeginVertical(GUI.skin.box);
showMaterialSlots[index] = EditorGUILayout.Foldout(showMaterialSlots[index], "Item slot " + index);
if (showMaterialSlots[index]) {
EditorGUILayout.PropertyField(possibleObjectsToCreateProperty.GetArrayElementAtIndex(index));
EditorGUILayout.PropertyField(levelOfMachineToCreateThisObjectProperty.GetArrayElementAtIndex(index));
EditorGUILayout.PropertyField(neededMaterialsProperty.GetArrayElementAtIndex(index));
}
EditorGUILayout.EndVertical();
}
}
How can I customize the code to show what I expect? I'm trying to change it for 2 days without any luck.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Add ScriptableObject to an List 0 Answers
Reliable way to detect when the game starts playing on a ScriptableObject? 1 Answer
How to Update ScriptableObject 1 Answer