Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

need help customizing inspector based on enum value

Discussion in 'Scripting' started by GarrettDR, Nov 1, 2020.

  1. GarrettDR

    GarrettDR

    Joined:
    Feb 24, 2019
    Posts:
    9
    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.

    Code (CSharp):
    1. public class Gameboard : MonoBehaviour
    2. {
    3.     public GameSpace[] gamespaces;
    4. }
    5.  
    6. [System.Serializable]
    7. public class GameSpace
    8. {
    9.     public enum spaceType
    10.     {
    11.         Action,
    12.         Property
    13.     }
    14.  
    15.     public spaceType SpaceType;
    16.     public string SpaceName;
    17.  
    18.     // Action Variables
    19.     public int MoveValue;
    20.     public int TurnValue;
    21.  
    22.     // Property Variables
    23.     public double Value;
    24.     public bool isAvailable;
    25.  
    26. }
     

    Attached Files:

  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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.

    Some related resources:
    https://riptutorial.com/unity3d/example/8282/custom-property-drawer
    https://catlikecoding.com/unity/tutorials/editor/custom-data/
     
  3. GarrettDR

    GarrettDR

    Joined:
    Feb 24, 2019
    Posts:
    9
    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.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6. //Your Class
    7. public class enumInspector : MonoBehaviour {
    8.      public enum MoveType { AutoMove, Waypoints };
    9.      public MoveType moveType;
    10.      //Auto Move variables
    11.      public Vector3 autoMoveDir;
    12.      public float autoMoveSpeed;
    13.      //waypoint varialbes
    14.      public Vector3[] waypoints;
    15.      public bool loopAtEnd;
    16. } //end of class
    17. //Custom inspector starts here
    18. #if UNITY_EDITOR
    19. [CustomEditor(typeof(enumInspector))]
    20. public class enumInspectorEditor : Editor {
    21.    
    22.      public override void OnInspectorGUI()
    23.      {
    24.          //cast target
    25.          var enumScript = target as enumInspector;
    26.          //Enum drop down
    27.          enumScript.moveType = (enumInspector.MoveType)EditorGUILayout.EnumPopup(enumScript.moveType);
    28.          //switch statement for different variables
    29.          switch (enumScript.moveType) {
    30.              //AutoMove
    31.              case enumInspector.MoveType.AutoMove:
    32.                  enumScript.autoMoveDir = EditorGUILayout.Vector3Field("Direction", enumScript.autoMoveDir); //Vector3 example
    33.                  enumScript.autoMoveSpeed = EditorGUILayout.FloatField("Speed", enumScript.autoMoveSpeed); //float example
    34.                  break;
    35.             //waypoint
    36.              case enumInspector.MoveType.Waypoints:
    37.                  enumScript.loopAtEnd = EditorGUILayout.Toggle("Loop", enumScript.loopAtEnd);//bool example
    38.                  //array example
    39.                  SerializedProperty waypointsProperty = serializedObject.FindProperty("waypoints"); //get array as Serialized Property
    40.                  EditorGUI.BeginChangeCheck(); //Check if the array inspector is dropped down
    41.                  EditorGUILayout.PropertyField(waypointsProperty, true); //array example (works with any Serialized property)
    42.                  if (EditorGUI.EndChangeCheck()) //End Array inspector dropped down
    43.                      serializedObject.ApplyModifiedProperties();
    44.                  break;
    45.          }//end switch
    46.      }
    47. }//end inspectorclass
    48. #endif
     
    knharsha and IBN_E_BAQIR like this.