Search code examples
c#unity-game-engineunity-editor

Unity Editor Option Popup? ( Unity Editor update Option )


The problem is I have many different options that only happen once. I have a dropdown menu that has two options. ( Slider, CheckBox )

!!Slider!! If the slider is selected I want to have the following options available in editor. ( MinValue, MaxValue, defaultValue)

!!CheckBox!! if checkbox is selected I want to have the following option in editor. ( Check )

z.b: ttps://drive.google.com/file/d/1Bwf1NukaFKoc5EHji_9YG5BoADN3RJzP/view?usp=sharing

I have already tried to influence the editor window. (https://docs.unity3d.com/ScriptReference/EditorGUILayout.FadeGroupScope.html) But since I only want to extend one option and do not want to create a new window, this option has not been very helpful.

The question is, how do I design more options and take them away? Unity version. 2018.4.8f1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;




public class uitest : MonoBehaviour
{
    public enum TestType { Slider, CheckBox };
    public TestType test = TestType.Slider;
    //[SerializeField]
    private bool Check = false;
    //[SerializeField]
    private int MinValue = 0;
    //[SerializeField]
    private int MaxValue = 3;
    //[SerializeField]
    private int defaultValue = 2;

}

Solution

  • You have to provide your own Editor for your type. Place it as uitesteditor.cs under directory named Editor somewhere in Assets.

    With public members (or accessors) this code can be done without too much Unity voodoo.

    using UnityEditor;
    using UnityEngine;
    
    [CustomEditor(typeof(uitest))]
    [CanEditMultipleObjects]
    public class uitesteditor : Editor
    {
        static string[] customProperties = new string[] { "Check", "MinValue", "MaxValue", "defaultValue" };
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            // Draw common properties (by excluding all custom ones)
            // Could be skipped if there is none such.
            DrawPropertiesExcluding(serializedObject, customProperties);
    
            var uitarget = target as uitest;
            // Custom UI based on selected enum
            switch (uitarget.test)
            {
                case uitest.TestType.CheckBox:
                    uitarget.Check = EditorGUILayout.Toggle("Check", uitarget.Check);
                    break;
                case uitest.TestType.Slider:
                    uitarget.MinValue = EditorGUILayout.IntField("Min value", uitarget.MinValue);
                    uitarget.MaxValue = EditorGUILayout.IntField("Max value", uitarget.MaxValue);
                    uitarget.defaultValue = EditorGUILayout.IntField("Default value", uitarget.defaultValue);
                    break;
            }
    
            // Needed only by DrawPropertiesExcluding
            serializedObject.ApplyModifiedProperties();
        }
    }
    

    If you want to operate on private fields with SerializeField there is more boilerplate code needed. We use SerializedProperty instances to access private serialized fields, thus code may look less readable.

    using UnityEditor;
    using UnityEngine;
    
    [CustomEditor(typeof(uitest))]
    [CanEditMultipleObjects]
    public class uitesteditor : Editor
    {
        static string[] customProperties = new string[] { "test", "Check", "MinValue", "MaxValue", "defaultValue" };
        SerializedProperty test, check, MaxValue, MinValue, defaultValue;
        private void OnEnable()
        {
            test = serializedObject.FindProperty("test");
            check = serializedObject.FindProperty("Check");
            MinValue = serializedObject.FindProperty("MinValue");
            MaxValue = serializedObject.FindProperty("MaxValue");
            defaultValue = serializedObject.FindProperty("defaultValue");
        }
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            // Draw common properties (by excluding all custom ones)
            DrawPropertiesExcluding(serializedObject, customProperties);
                        EditorGUILayout.PropertyField(test);
            switch ((uitest.TestType)test.intValue)
            {
                case uitest.TestType.CheckBox:
                    EditorGUILayout.PropertyField(check);
                    break;
                case uitest.TestType.Slider:
                    EditorGUILayout.PropertyField(MinValue);
                    EditorGUILayout.PropertyField(MaxValue);
                    EditorGUILayout.PropertyField(defaultValue);
                    break;
            }
    
            serializedObject.ApplyModifiedProperties();
        }
    }
    

    Note: I've added CanEditMultipleObjects tag, but you should decide on your own if it is desired. Rendered inspector GUI will use test enum value from first object selected.