简体   繁体   English

如何从新的MonoBehaviour隐藏脚本字段

[英]How hide script field from new MonoBehaviour

I'm want to hide script field from new scripts. 我想从新脚本中隐藏脚本字段。 (script are shown by default by every script) (每个脚本默认显示脚本)

在此处输入图片说明

One way is writing CustomEditor for our script. 一种方法是为我们的脚本编写CustomEditor

but i want faster way do that. 但是我想要更快的方法。 without writing editor script for my new script. 无需为新脚本编写编辑器脚本。

I suggest that create new attribute for more control. 我建议创建新属性以进行更多控制。

using System;

[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class HideScriptField : Attribute { }

And as editor script for all script that has this new attribute 作为具有此新属性的所有脚本的编辑器脚本

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(MonoBehaviour), true)]
public class DefaultMonoBehaviourEditor : Editor
{
    private bool hideScriptField;

    private void OnEnable()
    {
        hideScriptField = target.GetType().GetCustomAttributes(typeof(HideScriptField), false).Length > 0;
    }

    public override void OnInspectorGUI()
    {
        if (hideScriptField)
        {
            serializedObject.Update();
            EditorGUI.BeginChangeCheck();
            DrawPropertiesExcluding(serializedObject, "m_Script");
            if (EditorGUI.EndChangeCheck())
                serializedObject.ApplyModifiedProperties();
        }
        else
        {
            base.OnInspectorGUI();
        }
    }
}

Now in every new MonoBehaviour script if you want to hide script field you can add simply this attribute ( HideScriptField ) on it. 现在,在每个新的MonoBehaviour脚本中,如果要隐藏脚本字段,都可以在其上简单地添加此属性( HideScriptField )。 and done. 并做了。

From what I understand from your extremely poorly worded question I managed to conclude that you need attributes 根据我对您措辞极差的问题的理解,我设法得出结论,您需要属性

[SerializeField]
private CustomClass x;

is a private field that is shown in inspector. 是在检查器中显示的私有字段。

[HideInInspector]
public CustomClass x;

is a public field that is not visible in inspector. 是在检查器中不可见的公共字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM