Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
    5
    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. }