I am trying to figure out how to do this, please can someone who knows what they are doing point me in the right direction? When I select action in the ‘Space Type’, I only want ‘Move Value’ and ‘Turn Value’ displayed and when ‘Property’ is selected, I only want ‘Value’ and ‘Is Available’ displayed. I have found some code that works with an Enum, but I am trying to figure out how to do it with an array. Thank you for any help provided.
public class Gameboard : MonoBehaviour
{
public GameSpace[] gamespaces;
}
[System.Serializable]
public class GameSpace
{
public enum spaceType
{
Action,
Property
}
public spaceType SpaceType;
public string SpaceName;
// Action Variables
public int MoveValue;
public int TurnValue;
// Property Variables
public double Value;
public bool isAvailable;
}
You need to create a custom property drawer for your GameSpace class, in this case.
In the property drawer’s OnGUI method, check the current value of SpaceType, and then draw the corresponding properties you wish to.
Fair warning, it’s not really as simple as “just checking and displaying the correct properties”; you will have to manually draw the entire UI layout of GameSpace’s inspector.
Thank you Vryken for your response. Yeah, I found some code that helped with what I needed but I cannot figure out how to combine the two to get the desired result. I want to say how many spaces, select the type of space it is, Action or Property, and then have the relate variables displayed. I have spent all day and have accomplished. So frustrating! This code was provided by ‘TheBlackBurrito’ on an answers.unity.com thread.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//Your Class
public class enumInspector : MonoBehaviour {
public enum MoveType { AutoMove, Waypoints };
public MoveType moveType;
//Auto Move variables
public Vector3 autoMoveDir;
public float autoMoveSpeed;
//waypoint varialbes
public Vector3[] waypoints;
public bool loopAtEnd;
} //end of class
//Custom inspector starts here
#if UNITY_EDITOR
[CustomEditor(typeof(enumInspector))]
public class enumInspectorEditor : Editor {
public override void OnInspectorGUI()
{
//cast target
var enumScript = target as enumInspector;
//Enum drop down
enumScript.moveType = (enumInspector.MoveType)EditorGUILayout.EnumPopup(enumScript.moveType);
//switch statement for different variables
switch (enumScript.moveType) {
//AutoMove
case enumInspector.MoveType.AutoMove:
enumScript.autoMoveDir = EditorGUILayout.Vector3Field("Direction", enumScript.autoMoveDir); //Vector3 example
enumScript.autoMoveSpeed = EditorGUILayout.FloatField("Speed", enumScript.autoMoveSpeed); //float example
break;
//waypoint
case enumInspector.MoveType.Waypoints:
enumScript.loopAtEnd = EditorGUILayout.Toggle("Loop", enumScript.loopAtEnd);//bool example
//array example
SerializedProperty waypointsProperty = serializedObject.FindProperty("waypoints"); //get array as Serialized Property
EditorGUI.BeginChangeCheck(); //Check if the array inspector is dropped down
EditorGUILayout.PropertyField(waypointsProperty, true); //array example (works with any Serialized property)
if (EditorGUI.EndChangeCheck()) //End Array inspector dropped down
serializedObject.ApplyModifiedProperties();
break;
}//end switch
}
}//end inspectorclass
#endif