SHARE
TWEET

Unity MeshCombine script

Huvaakoodia Apr 23rd, 2016 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class MeshCombine : MonoBehaviour
  4. {
  5.     public void CombineMeshes(MeshFilter[] meshFilters)
  6.     {
  7.         CombineInstance[] combine = new CombineInstance[meshFilters.Length];
  8.         int i = 0;
  9.         while (i < meshFilters.Length)
  10.         {
  11.             combine[i].mesh = meshFilters[i].sharedMesh;
  12.             combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
  13.             Destroy(meshFilters[i].GetComponent<MeshRenderer>());
  14.             Destroy(meshFilters[i]);
  15.             i++;
  16.         }
  17.         transform.GetComponent<MeshFilter>().mesh = new Mesh();
  18.         transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine, true, true);
  19.         transform.GetComponent<MeshFilter>().mesh.Optimize();
  20.     }
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top