当我打电话时,CombineMeshes()
我收到 431 次错误:
Cannot combine mesh that does not allow access: Cube
UnityEngine.Mesh:CombineMeshes(CombineInstance[])
Run Code Online (Sandbox Code Playgroud)
我已经读过这个和这个。我正在使用 Unity 2019.1.0f2
- 因此如果我想使用 Blender ,我就被迫使用 Blender 2.80beta
。
我制作了一个非常简单的立方体(实际上简单得不能再简单了),并将其作为文件导出到 Unity 中fbx
:
Wall
我这样称呼它:
由此我制作了一个预制件。然后我创建了一个空对象,创建了一个脚本来生成墙壁。它实例化预制件来建造墙壁,这给出了:
但那些预制件是像这样的简单立方体:
所以我想将它们合并。Unity 为此准备了一些东西:CombineMeshes()
。
我尝试调整链接中的代码CombineMeshes()
,所以这是我的完整脚本,非常简单:它实例化并尝试在最后合并所有内容:
using System;
using System.Linq;
using UnityEngine;
public class CoinGenerator : MonoBehaviour
{
public GameObject wallPrefab;
public float gridSize = 40f;
public float topPrefab = 60f;
private void Start()
{
string[] ok = {
"+------------+ +------------+",
"|............| |............|",
"|.+--+.+---+.| |.+---+.+--+.|",
"|.| |.| |.| |.| |.| |.|",
"|.+--+.+---+.+-+.+---+.+--+.|",
"|............. .............|",
"|.+--+.++.+-------+.++.+--+.|",
"|.+--+.||.+--+ +--+.||.+--+.|",
"|......||....| |....||......|",
"+----+.|+--+.| |.+--+|.+----+",
" |.|+--+.+-+.+--+|.| ",
" |.||..... .....||.| ",
" |.||.+--- ---+.||.| ",
"-----+.++.| |.++.+-----",
"..........| |..........",
"-----+.++.| |.++.+-----",
" |.||.+-------+.||.| ",
" |.||...........||.| ",
" |.||.+-------+.||.| ",
"+----+.++.+--+ +--+.++.+----+",
"|............| |............|",
"|.+--+.+---+.| |.+---+.+--+.|",
"|.+-++.+---+.+-+.+---+.++-+.|",
"|...||........ ........||...|",
"+-+.||.++.+-------+.++.||.+-+",
"+-+.++.||.+--+ +--+.||.++.+-+",
"+......||....| |....||......+",
"+.+----++--+.| |.+--++----+.+",
"+.+--------+.+-+.+--------+.+",
"+...........................+",
"+---------------------------+"
};
MeshFilter[] meshFilters = {};
for (int z = -14; z <= 16; z++) {
for (int x = -14; x <= 14; x++) {
char c = ok[30 - (z + 14)][x + 14];
GameObject cp = null;
if (c == '+' || c == '-' || c == '|') {
cp = Instantiate(wallPrefab, null, true);
MeshFilter[] m = cp.GetComponentsInChildren<MeshFilter>();
meshFilters = meshFilters.Concat(m).ToArray();
}
if (cp == null) {
continue;
}
cp.transform.position = new Vector3(
x * gridSize, topPrefab, z * gridSize
);
}
}
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length) {
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
// hide the objects, they will be merges into one:
//meshFilters[i].gameObject.SetActive(false);
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
}
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时,CombineMeshes()
我收到 431 次错误:
Cannot combine mesh that does not allow access: Cube
UnityEngine.Mesh:CombineMeshes(CombineInstance[])
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?