kingBook

导航

Unity Editor 控制检视面板的属性是否显示和绘制只读属性

image

TestInspectorPropertyVisible.cs

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

public class TestInspectorPropertyVisible : MonoBehaviour {

    [System.Serializable]
    public class AdvancedSetting {
        public bool smoothing;
        public int id;
    }

    [SerializeField] private bool m_visibleAdvancedSetting;
    [SerializeField] private AdvancedSetting m_advancedSetting;
    [SerializeField] private string m_content;

}

EditorTestInspectorPropertyVisible.cs

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

[CustomEditor(typeof(TestInspectorPropertyVisible))]
public class EditorTestInspectorPropertyVisible : Editor {

    private SerializedProperty m_scriptProp;
    //
    private SerializedProperty m_visibleAdvancedSettingProp;
    private SerializedProperty m_advancedSettingProp;
    private SerializedProperty m_contentProp;

    private void OnEnable () {
        m_scriptProp = serializedObject.FindProperty("m_Script");
        //
        m_visibleAdvancedSettingProp = serializedObject.FindProperty("m_visibleAdvancedSetting");
        m_advancedSettingProp = serializedObject.FindProperty("m_advancedSetting");
        m_contentProp = serializedObject.FindProperty("m_content");
    }

    public override void OnInspectorGUI () {
        serializedObject.Update();
        // 设置 GUI.enabled,绘制 ‘Script’ 为只读属性
        GUI.enabled = false;
        EditorGUILayout.PropertyField(m_scriptProp);
        GUI.enabled = true;
        {
            EditorGUILayout.PropertyField(m_visibleAdvancedSettingProp);
            if (m_visibleAdvancedSettingProp.boolValue) {
                EditorGUILayout.PropertyField(m_advancedSettingProp);
            }
            EditorGUILayout.PropertyField(m_contentProp);
        }
        serializedObject.ApplyModifiedProperties();
    }
}

posted on 2022-02-21 22:02  kingBook  阅读(410)  评论(0编辑  收藏  举报