static private void MakeBatch(List <MeshSubsetCombineUtility.MeshContainer> meshes, Transform staticBatchRootTransform, int batchIndex)
{
if (meshes.Count < 2)
{
return;
}
List <MeshSubsetCombineUtility.MeshInstance> meshInstances = new List <MeshSubsetCombineUtility.MeshInstance>();
List <MeshSubsetCombineUtility.SubMeshInstance> allSubMeshInstances = new List <MeshSubsetCombineUtility.SubMeshInstance>();
foreach (MeshSubsetCombineUtility.MeshContainer mesh in meshes)
{
meshInstances.Add(mesh.instance);
allSubMeshInstances.AddRange(mesh.subMeshInstances);
}
string combinedMeshName = CombinedMeshPrefix;
combinedMeshName += " (root: " + ((staticBatchRootTransform != null) ? staticBatchRootTransform.name : "scene") + ")";
if (batchIndex > 0)
{
combinedMeshName += " " + (batchIndex + 1);
}
Mesh combinedMesh = StaticBatchingHelper.InternalCombineVertices(meshInstances.ToArray(), combinedMeshName);
StaticBatchingHelper.InternalCombineIndices(allSubMeshInstances.ToArray(), combinedMesh);
int totalSubMeshCount = 0;
foreach (MeshSubsetCombineUtility.MeshContainer mesh in meshes)
{
// Changing the mesh resets the static batch info, so we have to assign sharedMesh first
MeshFilter filter = (MeshFilter)mesh.gameObject.GetComponent(typeof(MeshFilter));
filter.sharedMesh = combinedMesh;
int subMeshCount = mesh.subMeshInstances.Count();
Renderer renderer = mesh.gameObject.GetComponent <Renderer>();
renderer.SetStaticBatchInfo(totalSubMeshCount, subMeshCount);
renderer.staticBatchRootTransform = staticBatchRootTransform;
// For some reason if GOs were created dynamically
// then we need to toggle renderer to avoid caching old geometry
renderer.enabled = false;
renderer.enabled = true;
// Remove the additionalVertexStreamsMesh, all its data has been copied into the combined mesh.
MeshRenderer meshRenderer = renderer as MeshRenderer;
if (meshRenderer != null)
{
meshRenderer.additionalVertexStreams = null;
}
totalSubMeshCount += subMeshCount;
}
}