简体   繁体   English

如何隐藏[SerializeField]私有变量,它继承自Unity3d Inspector中的基类

[英]How to hide the private variable with [SerializeField] inherit from the base class in the Inspector in Unity3d

When I have a private variable id , I write [SerializeField] before i define it: 当我有一个private变量id ,我在定义它之前先写了[SerializeField]

class A{
   [SerializeField]
   private int id;
}

And, I have another class 而且,我还有另一堂课

Class B : A{
}

When I add the component B on one object, I can see variable id in the inspector, How can I hide it? 将组件B添加到一个对象上时,可以在检查器中看到变量id ,该如何隐藏它?

Writing a custom inspector is fine, but often tedious if the only thing you need to change is a visibility of a serialized field. 编写自定义检查器很好,但是如果您唯一需要更改的是序列化字段的可见性,则通常很乏味。

Try [HideInInspector] attribute before a serialized fields. 在序列化字段之前尝试[HideInInspector]属性。 It will be serialized but not shown in the inspector. 它会被序列化,但不会在检查器中显示。


EDIT 编辑

Here's a couple of reference to understand serialization: ref1 , ref2 . 这里有一些参考来理解序列化: ref1ref2

I'll try to explain in brief: 我将尝试简要解释一下:

The process of setting the fields of your objects, and getting them, is called deserialization and serialization respectively. 设置对象字段并获取它们的过程分别称为反序列化和序列化。 Unity's serializer is able to serialize many different kinds of fields, but not all of them. Unity的序列化程序能够序列化许多不同种类的字段,但不是全部。

In a few words: 简而言之:

Serialization 序列化

Is the term used to indicate every data flow from the runtime memory to outside (such inspectors, files on the disk, ...) 是用于表示从运行时内存到外部的每个数据流的术语(例如检查器,磁盘上的文件等)。

Serialization 序列化

Is the term used to indicate every data flow towards the runtime memory to outside (es. change a value in the inspector and the serializer thread will deserialize it into the runtime memory). 是该术语,用于指示流向外部运行时内存的每个数据流(例如,在检查器中更改一个值,而串行化程序线程会将其反序列化到运行时内存中)。

The main point is that there are situations where you want a given field to be serialized (es. saved into a prefab on disk) but don't want to show it in the inspector. 要点是,在某些情况下,您希望将给定的字段序列化(例如保存到磁盘上的预制件中),但又不想在检查器中显示。 If this is the case, rewriting a whole inspector is an overkill. 在这种情况下,改写整个检查员就太过分了。 Use instead [HideInInspector] attribute. 改用[HideInInspector]属性。

If you want it not to be serialized nor shown in the inspector, just let it private without [SerializedField] or public and mark it as [NonSerialized] . 如果您不希望其序列化或不显示在检查器中,只需将其设为不带[SerializedField]或public并将其标记为[NonSerialized]即可

我认为唯一的方法是构建自定义检查器: http : //unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

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

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