diff --git a/Runtime/Core/Scripts/Utils/AvatarMeshHelper.cs b/Runtime/Core/Scripts/Utils/AvatarMeshHelper.cs index c1b12a0f..78f4b0f7 100644 --- a/Runtime/Core/Scripts/Utils/AvatarMeshHelper.cs +++ b/Runtime/Core/Scripts/Utils/AvatarMeshHelper.cs @@ -7,7 +7,8 @@ namespace ReadyPlayerMe.Core { public static class AvatarMeshHelper { - private static readonly string[] headMeshNames = { + private static readonly string[] headMeshNames = + { "Renderer_Head", "Renderer_Hair", "Renderer_Beard", @@ -16,7 +17,7 @@ public static class AvatarMeshHelper "Renderer_EyeLeft", "Renderer_EyeRight", "Renderer_Headwear", - "Renderer_Facewear", + "Renderer_Facewear" }; [Obsolete("Use TransferMesh(GameObject source, GameObject target) instead.")] @@ -24,7 +25,7 @@ public static void TransferMesh(GameObject source, SkinnedMeshRenderer[] targetM { TransferMesh(source, targetMeshes[0].transform.parent.gameObject); } - + /// /// Transfers the mesh and material data from the source to the target avatar. /// @@ -34,15 +35,17 @@ public static void TransferMesh(GameObject source, GameObject target) { // store the relevant data of the source (downloaded) avatar var rendererDict = new Dictionary(); - + var sourceRenderers = source.GetComponentsInChildren(); var targetRenderers = target.GetComponentsInChildren(); - + foreach (var renderer in sourceRenderers) { rendererDict.Add(renderer.name, renderer); } - + + var meshWithBones = targetRenderers.DefaultIfEmpty(null).FirstOrDefault((renderer) => renderer.bones.Length != 0); + // transfer the data to the target skinning mesh renderers foreach (var renderer in targetRenderers) { @@ -50,7 +53,12 @@ public static void TransferMesh(GameObject source, GameObject target) { renderer.sharedMesh = sourceRenderer.sharedMesh; renderer.sharedMaterial = sourceRenderer.sharedMaterial; - + + if (renderer.bones.Length == 0 && meshWithBones != null) + { + renderer.bones = meshWithBones.bones; + continue; + } // transfer the bone data foreach (var targetBone in renderer.bones) { @@ -61,7 +69,7 @@ public static void TransferMesh(GameObject source, GameObject target) targetBone.position = sourceBone.position; targetBone.rotation = sourceBone.rotation; targetBone.localScale = sourceBone.localScale; - break; + break; } } } @@ -72,13 +80,13 @@ public static void TransferMesh(GameObject source, GameObject target) renderer.material = null; } } - + // transfer the animation avatar var sourceAnimator = source.GetComponentInChildren(); var targetAnimator = target.GetComponentInChildren(); targetAnimator.avatar = sourceAnimator.avatar; } - + /// /// Returns the meshes of the head of the avatar, such as glasses, hair, teeth, etc. /// @@ -88,7 +96,7 @@ public static GameObject[] GetHeadMeshes(GameObject avatar) { var renderers = avatar.GetComponentsInChildren(); var headMeshes = new List(); - + foreach (var renderer in renderers) { if (headMeshNames.Contains(renderer.name)) @@ -96,7 +104,7 @@ public static GameObject[] GetHeadMeshes(GameObject avatar) headMeshes.Add(renderer.gameObject); } } - + return headMeshes.ToArray(); } }