繁体   English   中英

从游戏对象UNITY C#创建网格

[英]Create a mesh from gameobjects UNITY C#

我的场景中产生了许多游戏对象。 有没有一种方法可以创建一个网格来连接所有点,从而形成一个实体网格? 我已经研究了蒙皮网格物体,但是我不确定如何将其用于此目的。

谢谢。

我想与网格物体连接在一起的游戏对象

您可以使用Mesh.CombineMeshes函数从多个网格或Mesh.CombineMeshes创建一个网格。 首先,创建一个名为“ dots”的标签,并确保这些对象具有此标签,以使其更易于查找。 使用GameObject.FindGameObjectsWithTag通过标记查找所有点GameObject.FindGameObjectsWithTag 创建CombineInstance数组,并使用mesh初始化每个CombineInstance并从每个点transform MeshFilter信息。

创建新的GameObject来容纳新的组合对象,然后将MeshFilterMeshRenderer附加到其上。 将材料应用到它。 最后,使用MeshFilter.CombineMeshes合并存储在CombineInstance所有那些网格。

void CombineDotMeshes(Material mat)
{
    //Find all the dots GameObjects
    GameObject[] allDots = GameObject.FindGameObjectsWithTag("dots");

    //Create CombineInstance from the amount of dots
    CombineInstance[] cInstance = new CombineInstance[allDots.Length];

    //Initialize CombineInstance from MeshFilter of each dot
    for (int i = 0; i < allDots.Length; i++)
    {
        //Get current Mesh Filter and initialize each CombineInstance 
        MeshFilter cFilter = allDots[i].GetComponent<MeshFilter>();

        //Get each Mesh and position
        cInstance[i].mesh = cFilter.sharedMesh;
        cInstance[i].transform = cFilter.transform.localToWorldMatrix;
        //Hide each MeshFilter or Destroy the GameObject 
        cFilter.gameObject.SetActive(false);
    }

    //Create new GameObject that will contain the new combined Mesh
    GameObject combinedMesh = new GameObject("CombinedDots");
    MeshRenderer mr = combinedMesh.AddComponent<MeshRenderer>();
    mr.material = mat;
    MeshFilter mf = combinedMesh.AddComponent<MeshFilter>();

    //Create new Mesh then combine it
    mf.mesh = new Mesh();
    mf.mesh.CombineMeshes(cInstance);
}

用法

public Material mat;

void Start()
{
    CombineDotMeshes(mat);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM