BulkMeshBoneSwapper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using EasyButtons;
  5. public class BulkMeshBoneSwapper : MonoBehaviour
  6. {
  7. public Transform srcRootBone;
  8. [Button]
  9. void Recalculate()
  10. {
  11. SkinnedMeshRenderer[] meshes = GetComponentsInChildren<SkinnedMeshRenderer>();
  12. for (int x = 0; x < meshes.Length; x++)
  13. {
  14. if(meshes[x]==null){Debug.Log("Ignoring null object : " + meshes[x].name);continue;}
  15. // meshes[x].rootBone = srcRootBone;
  16. //target.bones = src.bones;
  17. Transform[] bonesT = new Transform[meshes[x].bones.Length];
  18. for (int i = 0; i < meshes[x].bones.Length; i++)
  19. {
  20. Transform targetBone = meshes[x].bones[i];
  21. if(targetBone==null){continue;}
  22. string targetBoneName = targetBone.name;
  23. foreach (Transform sourceBone in srcRootBone.GetComponentsInChildren<Transform>())
  24. {
  25. if (targetBoneName == sourceBone.name)
  26. {
  27. Debug.Log("Switching bone : " + targetBoneName);
  28. bonesT[i] = sourceBone;
  29. break;
  30. }
  31. }
  32. }
  33. meshes[x].bones = bonesT;
  34. if(meshes[x].sharedMesh==null){Debug.Log("Ignoring null mesh : " + meshes[x].name);continue;}
  35. //meshes[x].sharedMesh.RecalculateNormals();
  36. //meshes[x].sharedMesh.RecalculateBounds();
  37. }
  38. }
  39. }