如何在Unity Inspector中创建多维数组? [英] How to create multidimensional array in Unity Inspector?
问题描述
如何在Unity Inspector中创建一个枚举多维数组并使它可序列化,以便我可以从其他脚本中调用它?
How does one create an enum multidimensional array in Unity Inspector and make it serializable so I can call it from a different script?
public enum colors {red, blue, green, yellow, cyan, white, purple};
public int rows = 7;
public int column = 4;
public colors[,] blockColors;
private void Awake() {
blockColors = new colors[rows, column];
}
对于我来说,手动输入脚本中的所有28种颜色非常耗时,尤其是当我必须对数百个级别进行输入时.是否可以在Inspector中创建表以使工作流程更快?
For me to manually type all 28 colors in the script is time consuming, especially, when I have to do this for hundreds of levels. Is there a way to create a table in the Inspector to make workflow faster?
我尝试将blockColors
设置为[Serializefield]
,但是它不起作用.
我以前从未尝试过为检查员编写图表.有人可以将我引导到可以帮助我理解如何对图片中的图表进行编码的教程吗?
I tried making blockColors
a [Serializefield]
but it doesn't work.
I've never tried coding a chart for the inspector before. Can someone please direct me to a tutorial that can help me understand how to code a chart like in the picture?
推荐答案
您需要创建一个自定义编辑器(或更具体地讲,如果要将CustomPropertDrawer用于其他组件,则需要创建一个自定义编辑器
You need to create a custom editor (or more specifically CustomPropertDrawer if you want to re-use it for other components
创建这样的表所需的唯一非显而易见的部分是强制元素按所需方式布置.一种方法是手动处理Unity给您的Rect位置,但是有一种非常简单(尽管灵活性稍差)的方法,只需将您的元素包装在水平/垂直布局组合中即可. 直观的方法是将您的元素包装在
The only non-obvious part required to create a table like that is forcing the elements to lay out the way you want. One way is manually handling position Rect's given you by Unity, but there is a much simple (albeit a bit less flexible) way, just wrap your elements in horizontal/vertical layout combos. The intuitive approach would be to wrap your elements in
GUILayout.BeginHorizontal();
{
// your elements line 1
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
// your elements line 2 and so on
}
GUILayout.EndHorizontal();
,但是它有一个缺点-自动布局只会占用当前行中的所有元素,但是如果元素大小变化,则会破坏垂直倾斜度.一种解决方案是先将每一列包装在布局中,然后使用水平布局组合垂直条,
but it has a downside - autolayout will only take widts of elements in a current line, but if element size varies this will break vertical alingment. A solution is to wrap each column in a layout first, and than use the horizontal layout to combine vertical strips, it goes like this
GUILayout.BeginHorizontal();
{
GUILayout.BeginVertical();
{
// your elements column 1
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
{
// your elements column 2
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
括号只是为了清楚起见,它们什么也没做. 希望对您有帮助
Brackets are just for clarity, they do nothing. I hope this helps
这篇关于如何在Unity Inspector中创建多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!