Unity Editor different enumerations display different properties

using NUnit.Framework.Internal;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(DoorControl))]
public class DoorControlEditor : Editor
{
    private SerializedObject obj;
    private DoorControl doorControl;
    private SerializedProperty iterator;
    private List<string> propertyNames;
    private DoorType tryGetValue;
    private Dictionary<string, DoorType> specialPropertys
        = new Dictionary<string, DoorType>
        {
            //表示字段a只会在枚举值=typeA时显示
            { "Vec", DoorType.ROTATE },
            { "Agent", DoorType.MOVE },
            { "Temp", DoorType.MOVE },
        };
    void OnEnable()
    {
        obj = new SerializedObject(target);
        iterator = obj.GetIterator();
        iterator.NextVisible(true);
        propertyNames = new List<string>();
        do
        {
            propertyNames.Add(iterator.name);
        } while (iterator.NextVisible(false));
        doorControl = (DoorControl)target;
    }

    public override void OnInspectorGUI()
    {
        obj.Update();
        GUI.enabled = false;
        foreach (var name in propertyNames)
        {
            if (specialPropertys.TryGetValue(name, out tryGetValue)
                && tryGetValue != doorControl.doorType)
                continue;
            EditorGUILayout.PropertyField(obj.FindProperty(name));
            if (!GUI.enabled)//让第1次遍历到的 Script 属性为只读
                GUI.enabled = true;
        }
        obj.ApplyModifiedProperties();
    }
}

Guess you like

Origin blog.csdn.net/weixin_46711336/article/details/128096974