![](/img/trans.png)
[英]How to calculate the numbers from different gameObjects together in Unity c#
[英]Create a mesh from gameobjects UNITY C#
您可以使用Mesh.CombineMeshes
函数从多个网格或Mesh.CombineMeshes
创建一个网格。 首先,创建一个名为“ dots”的标签,并确保这些对象具有此标签,以使其更易于查找。 使用GameObject.FindGameObjectsWithTag通过标记查找所有点GameObject.FindGameObjectsWithTag
。 创建CombineInstance
数组,并使用mesh
初始化每个CombineInstance
并从每个点transform
MeshFilter
信息。
创建新的GameObject来容纳新的组合对象,然后将MeshFilter
和MeshRenderer
附加到其上。 将材料应用到它。 最后,使用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.