简体   繁体   English

我为什么要使用SerializeField?

[英]Why should I use SerializeField?

I have just started to learn C# and Unity, and there is one thing that I can not get used to: 我刚刚开始学习C#和Unity,有一件事我不习惯:

Why and when should I use [SerializeField] ? 为什么以及何时应该使用[SerializeField]

Is it bad to leave variables hard coded despite using [SerializeField] and have more text boxes in my unity interface? 尽管使用[SerializeField]并且在我的Unity界面中有更多文本框,将变量保留为硬编码是不是很糟糕?

Thank you in advance for your attention and your patience. 提前感谢您的关注和耐心。

Why and when should I use [SerializeField]? 为什么以及何时应该使用[SerializeField]?

Using the SerializeField attribute causes Unity to serialize any private variable. 使用SerializeField属性会导致Unity序列化任何private变量。 This doesn't apply to static variables and properties in C#. 这不适用于C#中的静态变量和属性。

You use the SerializeField attribute when you need your variable to be private but also want it to show up in the Editor. 当您需要将变量设置为private但又希望它在编辑器中显示时,可以使用SerializeField属性。

For example, this wouldn't show up in the Editor: 例如,这不会出现在编辑器中:

private float score;

And this is because it's a private variable but the one below should show up in the Editor: 这是因为它是一个private变量,但下面的变量应该出现在编辑器中:

[SerializeField]
private float score;

That's because you applied SerializeField to it and you're telling Unity to serialize it and show it in the Editor. 那是因为您将SerializeField应用于它并且您告诉Unity将其序列化并在编辑器中显示它。


Note that private variables has more to do with C# than Unity. 请注意, private变量更多地与C#相关,而不是Unity。 There is also public variable variables. 还有公共变量变量。 Marking your variable private means that you don't want another script to be able to access that variable. 将变量标记为private意味着您不希望其他脚本能够访问该变量。 There is also public qualifier. 还有public资格赛。 Marking your variable public means that you want your other scripts to be able to access that variable. 将变量标记为public意味着您希望其他脚本能够访问该变量。

Sometimes, you want other scripts to be able to access your variable from another script but you don't want the public variable to show up in the Editor. 有时,您希望其他脚本能够从另一个脚本访问您的变量,但您不希望public变量显示在编辑器中。 You can hide the public variable with the [HideInInspector] attribute. 您可以使用[HideInInspector]属性隐藏public变量。

This will show in the Editor: 这将在编辑器中显示:

public float score;

This will not show in the Editor: 不会在编辑器中显示:

[HideInInspector]
public float score;

Is it bad to leave variables hard coded despite using [SerializeField] and have more text boxes in my unity interface? 尽管使用[SerializeField]并且在我的Unity界面中有更多文本框,将变量保留为硬编码是不是很糟糕?

Yes, it's mostly bad especially for new users. 是的,这对于新用户来说尤其不好。 It shouldn't be a big deal for a long time Unity and C# programmer. 对于很长一段时间的Unity和C#程序员来说,这应该不是什么大问题。 The reason this is bad is because when you have the code below: 这是不好的原因是因为你有以下代码:

[SerializeField]
private float score = 5f;

The default value is 5 in the Editor. 编辑器中的默认值为5 Once you save the script this variable is now updated in the Editor as 5. The problem is that you can change this from the Editor to 14 . 保存脚本后,此变量现在在编辑器中更新为5.问题是您可以将其从编辑器更改为14 Once you change it from the Editor, the value in the script will still be 5 but when you run it, Unity will use the value you set in the Editor which is 14 . 从编辑器更改后,脚本中的值仍为5但运行时,Unity将使用您在编辑器中设置的值14 This can cause you so much time troubleshooting something that isn't even a problem just because there is a different value being used that is set in the Editor while you're expecting the default value set in the script to be used. 这可能会导致您花费大量时间来解决甚至不是问题的问题,因为在您期望使用脚本中设置的默认值时,编辑器中设置了不同的值。

The only way for for the score variable to reset back to it's default 5 variable is when you either rename the variable to something else or reset it from the Editor. score变量重置为默认值5变量的唯一方法是将变量重命名为其他变量或从编辑器重置变量。 It won't even change even when you change the value from 5 to 3 from the script. 即使从脚本中将值从5更改为3 ,它也不会更改。 It has to be renamed or reset from the Editor. 必须从编辑器重命名或重置它。 It's worth knowing but when you get used to Unity, you won't have to worry about this. 值得了解但是当你习惯Unity时,你不必担心这一点。

The [SerializeField] attribute is used to mark non-public fields as serializable: so that Unity can save and load those values (all public fields are serialized by default) even though they are not public. [SerializeField]属性用于将非公共字段标记为可序列化:以便Unity可以保存和加载这些值(默认情况下所有公共字段都是序列化的),即使它们不是公共的。

That's all it does. 就是这样。 You use it when you want that. 你想要的时候使用它。

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

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