unity2019 assetbundle实现SkinnedMesh合并换装

一、效果图

unity2019 assetbundle实现SkinnedMesh合并换装_第1张图片

二、美术资源设定

unity2019 assetbundle实现SkinnedMesh合并换装_第2张图片

三、具体实现

//第二参数是所有mesh上组件SkinnedMeshRenderer集合
private GameObject CreatPerson (GameObject root , List skinneds) {
        List combineInstances = new List ();
        List materials = new List ();
        List bones = new List ();

        Transform[] transforms = root.GetComponents ();

        foreach(SkinnedMeshRenderer smr in skinneds) {

            materials.AddRange (smr.sharedMaterials);
            bones.AddRange(smr.bones);
            for(int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++) {
                CombineInstance ci = new CombineInstance ();
                Mesh mesh1 = new Mesh();
                smr.BakeMesh(mesh1);
                mesh1.uv = smr.sharedMesh.uv;
                ci.mesh = mesh1;

                ci.subMeshIndex = sub;
                combineInstances.Add (ci);
            }
        }
        SkinnedMeshRenderer r = root.AddComponent ();
        Mesh mesh = new Mesh ();
        mesh.CombineMeshes(combineInstances.ToArray(), false, false);
        r.sharedMesh = mesh;
        r.bones = bones.ToArray ();
        r.materials = materials.ToArray ();
        r.rootBone = root.transform;

        return root;

源码下载

你可能感兴趣的:(unity)