无法在 Unity 中更改 Inspector 变量中的数组大小? [英] Unable to change array size in Inspector variable in Unity?
问题描述
据我所知,这是如何设置矢量大小"
public Color[] teamAColors = new Color[4];
但是当代码运行时它看起来像这样
我为 [4] 设置的数字似乎无关紧要,大小始终保持为 6.我什至不确定 6 数字来自哪里,因为我没有为该数字设置任何内容.>
我什至尝试过
public Color[] teamAColors;
然后让我的数组自动填充长度,但这也不会改变 6.
很简单
public Color[] teamAColors;
但是那个6"呢?不会消失"的价值?
但是您偶然发现了重置"GOTCHYA 团结一致!
只需尝试使用不同的变量名称...
public Color[] teste;
看到了吗?
就是这样,工作正常.您可以动态设置大小,只要不要在代码中输入大小.
秘密在这里:
Unity坚持"以复杂的方式序列化值.
如果您在代码中设置大小甚至一次,Unity记住"即使您随后更改了代码,也是如此.
有一种方法可以重置这个内部"value .. 寻找著名的小重置按钮.
它附在微小的 Cog 符号上.
要记住的关键规则是:
永远不要在代码中为 Unity 中的公共变量设置默认值!!
公共浮点数x;//做这个公共浮动 x=2f;//通常永远不要这样做
当你在做的时候,这里有一个非常方便的技巧.试试这个...
[Header(这有多好?")]公众持股量 x;
这是一个更酷的!试试吧!
[范围(2f,4f)]公众持股量 x;
From what I have read this is how a vector "Size" is set
public Color[] teamAColors = new Color[4];
But when the code is run it looks like this
It doesn't seem to matter what number I put for the [4], the Size always stays 6. I am not sure where the 6 number is even coming from as I have not set anything to that number.
I have even tried to
public Color[] teamAColors;
And then let my array auto populate the Length, but that doesn't change the 6 either.
It's simply
public Color[] teamAColors;
But what about that "6" value that "won't go away"?
BUT YOU'VE STUMBLED ON TO THE "RESET" GOTCHYA IN UNITY!
Just try it with a different variable name...
public Color[] teste;
See?
There it is, working fine. You can set the size dynamically, so long as you never put the size in the code.
Here is the secret:
Unity "holds on to" serialized values in a complicated way.
If you set the size in code even once, Unity "remembers" this even if you subsequently change the code.
There is a way to reset this "inner" value .. look for the famous tiny reset button.
It is attached to the tiny Cog symbol.
The critical rule to remember is this:
NEVER, EVER, HAVE A DEFAULT VALUE IN CODE FOR PUBLIC VARIABLES IN UNITY!!
public float x; // do this
public float x=2f; // generally NEVER DO THIS
While you're at it, here's an incredibly handy trick. Try this ...
[Header("How good is this?")]
public float x;
Here's an even cooler one! try it!
[Range(2f,4f)]
public float x;
这篇关于无法在 Unity 中更改 Inspector 变量中的数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!