Question by
ShwPrgmmr · Apr 24, 2020 at 03:02 PM ·
inspectorcustom editorcustom-inspectorstructcustom-editor
Custom Editor: Array of Structs with elements hidden by toggles
I've got an array of the following struct:
[System.Serializable] public struct FigureJointControls
{
public string name;
public GameObject obj; //the object associated with this joint
public Vector3 rotation; //the X, Y, adn Z rotations of the joint in degrees
public bool dependent;
public int parentIndex; //the index in the list of joint control objects of the parent object
public float multiplier;
}
This array is set up in the main function here:
public class BrachController : MonoBehaviour
{
private int tick;
public int cap;
public int numJoints;
public FigureJointControls[] primaryJointControls;
//etc. etc.
}
I want to display this array in the Inspector and have the bool "dependent" toggle the visibility of "parentIndex" and "multiplier", which are an Int Slider and a float field respectively. Here's what I currently have:
[CustomEditor(typeof(BrachController))]
public class BrachScriptEditor : Editor
{
override public void OnInspectorGUI()
{
var myScript = target as BrachController;
SerializedObject so = new SerializedObject(target);
SerializedProperty figJoints = so.FindProperty("primaryJointControls");
for (int i = 0; i < myScript.primaryJointControls.Length; i++)
{
myScript.primaryJointControls[i].dependent = EditorGUILayout.Toggle("Joint No. " + i.ToString(), myScript.primaryJointControls[i].dependent);
using (var group = new EditorGUILayout.FadeGroupScope(Convert.ToSingle(myScript.primaryJointControls[i].dependent)))
{
if (group.visible == true)
{
EditorGUI.indentLevel ++;
myScript.primaryJointControls[i].parentIndex = EditorGUILayout.IntSlider("Parent Joint Index ", myScript.primaryJointControls[i].parentIndex, 0, myScript.primaryJointControls.Length);
myScript.primaryJointControls[i].multiplier = EditorGUILayout.FloatField("Parent Joint Multiplier", myScript.primaryJointControls[i].multiplier);
EditorGUI.indentLevel--;
}
}
}
}
}
This doesn't produce the desired effect. Instead it just shows the toggles but doesn't allow me to edit the length of the array.
Comment