Last active
July 2, 2024 14:24
-
-
Save emilianavt/721cd4dd2d4a62ba54b002b63f894dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[ExecuteInEditMode] | |
public class ReassignBoneWeigthsToNewMesh : MonoBehaviour { | |
public Transform newArmature; | |
public string rootBoneName = "Hips"; | |
public bool resetPose = false; | |
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] != null && children[a] != null && bones[i].name == children[a].name) { | |
if (resetPose) { | |
children[a].transform.localPosition = bones[i].transform.localPosition; | |
children[a].transform.localRotation = bones[i].transform.localRotation; | |
children[a].transform.localScale = bones[i].transform.localScale; | |
} | |
bones[i] = children[a]; | |
break; | |
} | |
} | |
} | |
rend.bones = bones; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated from here.