Using Unityengine;
Using System.Collections;
Using System.Collections.Generic;
public class Conbineopmesh:monobehaviour {
void Start () {
Combinetomesh (This.gameobject);
}
public void Combinetomesh (Gameobject _go)
{
skinnedmeshrenderer[] SMR = _go. Getcomponentsinchildren<skinnedmeshrenderer> ();
list<combineinstance> lcom = new list<combineinstance> ();
list<material> Lmat = new list<material> ();
list<transform> Ltra = new list<transform> ();
for (int i = 0; i < SMR. Length; i++)
{
Lmat. AddRange (smr[i].materials);
Ltra. AddRange (Smr[i].bones);
for (int sub = 0; Sub < Smr[i].sharedmesh.submeshcount; sub++)
{
Combineinstance ci = new combineinstance ();
Ci.mesh = Smr[i].sharedmesh;
Ci.submeshindex = Sub;
Lcom. Add (CI);
}
Destroy (Smr[i].gameobject);
}
Skinnedmeshrenderer _r = _go. Getcomponent<skinnedmeshrenderer> ();
if (_r = = null)
{
_r = _go. Addcomponent<skinnedmeshrenderer> ();
}
_r.sharedmesh = new Mesh ();
_r.bones = Ltra. ToArray ();
_r.materials = new Material[]{lmat[0]};
_r.rootbone = _go.transform;
_r.sharedmesh.combinemeshes (lcom. ToArray (), true,false);
}
void Update () {
}
}
Optimize resources by combining mesh