Unity:无法合并不允许访问的网格:多维数据集

时间:2019-05-01 11:41:08

标签: unity3d

当我致电CombineMeshes()时,我得到431倍的错误:

Cannot combine mesh that does not allow access: Cube
UnityEngine.Mesh:CombineMeshes(CombineInstance[])

我已经读过this了。 我正在使用Unity 2019.1.0f2-因此,如果我想使用Blender,则不得不使用Blender 2.80beta

我制作了一个非常简单的多维数据集(实际上并不能更简单),并将其作为fbx文件导出到Unity:

blender simple cube

我这样称呼它为“ Wall

wall blender object

由此,我做了一个预制件。然后,我做了一个空的对象,创建了一个脚本来生成墙。实例化预制件来制造墙壁,这给出了:

wall generated

但是这些预制件是像这样的简单立方体:

wireframe view

所以我想将它们合并。 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);
    }
}

当我致电CombineMeshes()时,我得到431倍的错误:

Cannot combine mesh that does not allow access: Cube
UnityEngine.Mesh:CombineMeshes(CombineInstance[])

我在做什么错了?

1 个答案:

答案 0 :(得分:2)

我怀疑您从Blender导入的多维数据集模型没有将Read/Write Enabled标志设置为true。

为您导入的模型进入Model Tab,并确保将Read/Write Enabled设置为true并应用您的更改。

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