c# - 如何在检查器中隐藏继承的公共(public)变量?

标签 c# unity-game-engine attributes

我有2个类A和B,B类继承自A类。

假设 A 类有一个名为 Lives 的公共(public) int 变量,并且由于 B 类继承自 A 类,因此该变量 (Lives) 将显示在检查器中。

我想在检查器中隐藏 B 类的变量 (Lives)。很多人告诉我只需使用 [SerializeField] 属性将变量 (Lives) 设为私有(private),但这对我不起作用,因为很多情况下我仍然想从不同的脚本,例如当我使用 A 类时的 GameManager 脚本。

我能做到吗?我能看到的最接近的答案是这个链接Hide Public Variables 。 “查找最后一条评论”。

最佳答案

我不相信您可以通过任何简单的方式隐藏从基类继承的变量。如果您确实希望某个属性仅显示在基本检查器中,但隐藏在继承类检查器中,您可能需要编写一个自定义检查器。我冒昧地编写了一个编辑器脚本,允许您按名称隐藏检查器中的属性。

要将其应用到您自己的检查器面板,您可以使用我创建的帮助器类。

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

public static class EditorHelper
{
    public static List<SerializedProperty> GetExposedProperties(SerializedObject so, IEnumerable<string> namesToHide = null)
    {
        if (namesToHide == null) namesToHide = new string[] { };
        IEnumerable<FieldInfo> componentFields = so.targetObject.GetType().GetFields();
        List<SerializedProperty> exposedFields = new List<SerializedProperty>();

        foreach (FieldInfo info in componentFields)
        {
            bool displayInInspector = info.IsPublic && !Attribute.IsDefined(info, typeof(HideInInspector));
            displayInInspector = displayInInspector || (info.IsPrivate && Attribute.IsDefined(info, typeof(SerializeField)));
            displayInInspector = displayInInspector && !namesToHide.Contains(info.Name);

            if (displayInInspector){
                SerializedProperty prop = so.FindProperty(info.Name);
                if(prop != null)
                    exposedFields.Add(prop);
            }
        }

        return exposedFields;
    }
}

然后在您的 Unity 项目中创建一个名为“Editor”的文件夹并将此脚本放入其中。您需要进行的唯一更改是将“MyCustomScript”更改为您的类的名称。然后在 OnEnable 方法中修改隐藏属性列表以包含要隐藏的所有属性的名称。

using UnityEditor;
using System.Collections.Generic;


[CustomEditor(typeof(MyCustomScript))]
public class MyCustomScriptEditor : Editor
{
    private List<SerializedProperty> properties;

    private void OnEnable()
    {
        string[] hiddenProperties = new string[]{"SomeString2"}; //fields you do not want to show go here
        properties = EditorHelper.GetExposedProperties(this.serializedObject,hiddenProperties);
    }

    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI(); - (standard way to draw base inspector)

        //We draw only the properties we want to display here
        foreach (SerializedProperty property in properties)
        {
            EditorGUILayout.PropertyField(property,true);
        }
        serializedObject.ApplyModifiedProperties();
    }
}

希望这有帮助。

关于c# - 如何在检查器中隐藏继承的公共(public)变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58780325/

相关文章:

c# - C# 4.0 中的双重调度 - 动态关键字?

unity-game-engine - 如何让玩家能够在Unity中围绕立方体旋转相机?

php - 在 Magento 中以编程方式更新/编辑属性选项

ruby-on-rails - 如何在 Rails 中使用带有自定义 setter 方法的 update_attributes?

c# - 在部分 View 中存储 session

c# - linq to sql应该用于流量大的网站吗

c# - 为什么在 timer2 tick 事件中调用两个函数时应用程序运行如此缓慢?

c# - 使用 Xbox Controller 播放动画

iphone - Unity3D 游戏在 Xcode 9 中崩溃 - iOS 11

python - 让所有属性出现在 python 的 `__dict__` 方法上