summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsQuestLegacy/Assets/ReassignBoneWeigthsToNewMesh.cs
blob: 70baa83513fbebaea3960a8d43356446648eb890 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[ExecuteInEditMode]
public class ReassignBoneWeigthsToNewMesh : MonoBehaviour
{
    public Transform newArmature;
    public string rootBoneName = "Hips";
    public bool PressToReassign;

    void Update()
    {
        if (PressToReassign)
            Reassign();
        PressToReassign = false;
    }

    // [ContextMenu("Reassign Bones")]
    public void Reassign()
    {
        if (newArmature == null)
        {
            Debug.Log("No new armature assigned");
            return;
        }

        if (newArmature.Find(rootBoneName) == null)
        {
            Debug.Log("Root bone not found");
            return;
        }

        Debug.Log("Reassingning bones");
        SkinnedMeshRenderer rend = gameObject.GetComponent<SkinnedMeshRenderer>();
        Transform[] bones = rend.bones;

        rend.rootBone = newArmature.Find(rootBoneName);

        Transform[] children = newArmature.GetComponentsInChildren<Transform>();

        for (int i = 0; i < bones.Length; i++)
            for (int a = 0; a < children.Length; a++)
                if (bones[i].name == children[a].name)
                {
                    bones[i] = children[a];
                    break;
                }

        rend.bones = bones;
    }

}