How to make variables change according to the type of enumeration displayed in the compiler

First, create a new script and hang it on any object. The test script code is as follows:

public class Test: MonoBehaviour
{
    
    
    public enum TestType
    {
    
    
        type1,
        type2,
        type3,
    }

    public TestType type;
    public int type_01_Int;
    public string type_02_String;
    public List<bool> type_03_List;
}

At this time, the following situation will appear in the compiler:
insert image description here

Create another script named TestEditor, the specific code is as follows:

[CustomEditor(typeof(Test))]
public class TestEditor: Editor
{
    
    
    private SerializedObject test;
    private SerializedProperty type, type_01_Int, type_02_String, type_03_List;

    private void OnEnable()
    {
    
    
        test = new SerializedObject(target);
        type = test.FindProperty("type");
        type_01_Int = test.FindProperty("type_01_Int");
        type_02_String = test.FindProperty("type_02_String");
        type_03_List = test.FindProperty("type_03_List");
    }

    public override void OnInspectorGUI()
    {
    
    
        base.OnInspectorGUI();

        // 更新test脚本
        test.Update();

        EditorGUILayout.PropertyField(type);
        if (type.enumValueIndex == 0)
        {
    
    
            EditorGUILayout.PropertyField(type_01_Int);
        }
        else if (type.enumValueIndex == 1)
        {
    
    
            EditorGUILayout.PropertyField(type_02_String);
        }
        else if (type.enumValueIndex == 2)
        {
    
    
            EditorGUILayout.PropertyField(type_03_List);
        }

        // 将该脚本变量应用
        test.ApplyModifiedProperties();
    }
}

The script does not need to be hung on the object.

At this time, look at the display of the compiler again, and it will change to the following display:

insert image description here

At this point we can see that the upper part is the variable display of the first script, and the lower part is the display of the second script control.
Using this method can achieve the classified management of compiler variables, which is not redundant , and naturally does not need the display of the above part. Only the second script is needed to control the display part, so it is only necessary to remove the variables in the first script from the compilation. Just display it in the browser, modify the first script as follows:

public class ChangeInspectorAccordingTheType : MonoBehaviour
{
    
    
    public enum TestType
    {
    
    
        type1,
        type2,
        type3,
    }

    [HideInInspector]
    public TestType type;
    [HideInInspector]
    public int type_01_Int;
    [HideInInspector]
    public string type_02_String;
    [HideInInspector]
    public List<bool> type_03_List;
}

At the same time, just comment out a line of code that updates the test script in TestEditor.

As shown in the picture:

insert image description here

At this point, our goal has been achieved, and we can switch the enumeration type at will to view the effect.

Guess you like

Origin blog.csdn.net/weixin_44081533/article/details/118612824