[英]Create a mesh from gameobjects UNITY C#
I have many gameobjects spawned in my scene. 我的场景中产生了许多游戏对象。 Is there a way to create a mesh to connect all the dots to make a solid mesh?
有没有一种方法可以创建一个网格来连接所有点,从而形成一个实体网格? I have researched skinned meshed but i am not sure how to use it for this purpose.
我已经研究了蒙皮网格物体,但是我不确定如何将其用于此目的。
Thank you. 谢谢。
You can create one mesh from multiple meshes or GameObjects with the Mesh.CombineMeshes
function. 您可以使用
Mesh.CombineMeshes
函数从多个网格或Mesh.CombineMeshes
创建一个网格。 First, create a tag named "dots" and make sure those objects have this tag to make them easier to find. 首先,创建一个名为“ dots”的标签,并确保这些对象具有此标签,以使其更易于查找。 Find all the dot GameObjects by tag with
GameObject.FindGameObjectsWithTag
. 使用GameObject.FindGameObjectsWithTag通过标记查找所有点
GameObject.FindGameObjectsWithTag
。 Create array of CombineInstance
and initialize each CombineInstance
with the mesh
and transform
information of MeshFilter
from each dot. 创建
CombineInstance
数组,并使用mesh
初始化每个CombineInstance
并从每个点transform
MeshFilter
信息。
Create new GameObject to hold the new combined Objects then attach MeshFilter
and MeshRenderer
to it. 创建新的GameObject来容纳新的组合对象,然后将
MeshFilter
和MeshRenderer
附加到其上。 Apply a material to it. 将材料应用到它。 Finally, use
MeshFilter.CombineMeshes
to combine all those meshes stored in CombineInstance
. 最后,使用
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);
}
Usage : 用法 :
public Material mat;
void Start()
{
CombineDotMeshes(mat);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.