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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Discussion Custom Editor Enum Dropdown to display vars, but with a custom object?

Discussion in 'Editor & General Support' started by jeanmichelmorin, Feb 3, 2023.

  1. jeanmichelmorin

    jeanmichelmorin

    Joined:
    Jun 7, 2018
    Posts:
    4
    I followed a few guides which works well with types like string, int, float, bool etc... but if I have lets say my own type (InteractSteps) which englobes an int and a string, it doesn't work.

    As of right now, it doesn't do anything.

    Basically, I want this:


    Here's my Custom Editor code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. #endif
    8.  
    9. [CustomEditor(typeof(InteractActionableState))]
    10. public class InteractActionableStateEditor : Editor
    11. {
    12.     public EnumInteractActionableMode categoryToDisplay;
    13.  
    14.     public override void OnInspectorGUI()
    15.     {
    16.         base.OnInspectorGUI();
    17.         categoryToDisplay = (EnumInteractActionableMode)EditorGUILayout.EnumPopup("ModeAnimation", categoryToDisplay);
    18.  
    19.         EditorGUILayout.Space();
    20.  
    21.         switch (categoryToDisplay)
    22.         {
    23.             case EnumInteractActionableMode.Animator:
    24.                 DisplayAnimator();
    25.                 break;
    26.  
    27.             case EnumInteractActionableMode.Transforms:
    28.                 DisplayTransform();
    29.                 break;
    30.         }
    31.  
    32.         serializedObject.ApplyModifiedProperties();
    33.     }
    34.  
    35.     void DisplayAnimator()
    36.     {
    37.         EditorGUILayout.PropertyField(serializedObject.FindProperty("animClip"));
    38.         EditorGUILayout.PropertyField(serializedObject.FindProperty("AnimationGameObjectWithScriptToRun"));
    39.     }
    40.  
    41.     void DisplayTransform()
    42.     {
    43.         EditorGUILayout.PropertyField(serializedObject.FindProperty("Transforms"));
    44.     }
    45. }
    And here's my object code:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Events;
    6.  
    7. [Serializable]
    8. public class InteractActionableState
    9. {
    10.     [Space(10)]
    11.     [Header("=== Global ===")]
    12.     public GameObject requiredObjectForInteract;
    13.     public bool requiredObjectInHand = true;
    14.     public AudioClip soundOfState;
    15.     public bool DoExternalEventsDuringMovement = false;
    16.     public UnityEvent ExternalObjectExecuteFunction;
    17.     public EnumInteractActionableMode ModeAnimation;
    18.  
    19.     [Space(10)]
    20.     [Header("=== Mode Animation: Animator ===")]
    21.     public AnimationClip animClip;
    22.     public UnityEvent AnimationGameObjectWithScriptToRun;
    23.  
    24.     [Space(10)]
    25.     [Header("=== Mode Animation: Transform ===")]
    26.     public List<InteractionStep> Transforms = new List<InteractionStep>();
    27.  
    28.     public void TriggerExternalEvents()
    29.     {
    30.         ExternalObjectExecuteFunction.Invoke();
    31.     }
    32. }