Unity:无法组合不允许访问的网格:立方体

Oli*_*ons 5 unity-game-engine

当我打电话时,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)

我究竟做错了什么?

Dar*_*ane 7

我怀疑您从搅拌机导入的立方体模型没有将标志Read/Write Enabled设置为 true。

进入导入模型的模型选项卡,确保其Read/Write Enabled设置为 true 并应用您的更改。

如果已经设置了这个标志,那么我能想到的唯一一件事就是可能会调用UploadMeshData()您的代码中的某个位置(或者可能在您导入的包中?)。UploadMeshData()采用布尔值作为参数,如果为 true,则将网格设置为脚本不再可读。