|
1 | 1 | using UnityEngine;
|
| 2 | +using System.Collections.Generic; |
2 | 3 |
|
3 | 4 | namespace ReadyPlayerMe.Core
|
4 | 5 | {
|
5 | 6 | public static class AvatarMeshHelper
|
6 | 7 | {
|
| 8 | + [Obsolete("Use TransferMesh(GameObject source, GameObject target) instead.")] |
7 | 9 | public static void TransferMesh(GameObject source, SkinnedMeshRenderer[] targetMeshes, Animator targetAnimator)
|
8 | 10 | {
|
9 |
| - var sourceAnimator = source.GetComponentInChildren<Animator>(); |
10 |
| - SkinnedMeshRenderer[] sourceMeshes = source.GetComponentsInChildren<SkinnedMeshRenderer>(); |
11 |
| - for (var i = 0; i < targetMeshes.Length; i++) |
| 11 | + TransferMesh(source, targetMeshes[0].transform.parent.gameObject); |
| 12 | + } |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Transfers the mesh and material data from the source to the target avatar. |
| 16 | + /// </summary> |
| 17 | + /// <param name="source">Imported Ready Player Me avatar.</param> |
| 18 | + /// <param name="target">Photon Network Player Character.</param> |
| 19 | + public static void TransferMesh(GameObject source, GameObject target) |
| 20 | + { |
| 21 | + // store the relevant data of the source (downloaded) avatar |
| 22 | + var rendererDict = new Dictionary<string, SkinnedMeshRenderer>(); |
| 23 | + |
| 24 | + var sourceRenderers = source.GetComponentsInChildren<SkinnedMeshRenderer>(); |
| 25 | + var targetRenderers = target.GetComponentsInChildren<SkinnedMeshRenderer>(); |
| 26 | + |
| 27 | + foreach (var renderer in sourceRenderers) |
| 28 | + { |
| 29 | + rendererDict.Add(renderer.name, renderer); |
| 30 | + } |
| 31 | + |
| 32 | + // transfer the data to the target skinning mesh renderers |
| 33 | + foreach (var renderer in targetRenderers) |
12 | 34 | {
|
13 |
| - if (i >= sourceMeshes.Length) |
| 35 | + if (rendererDict.TryGetValue(renderer.name, out var sourceRenderer)) |
14 | 36 | {
|
15 |
| - targetMeshes[i].enabled = false; |
16 |
| - break; |
| 37 | + renderer.material = sourceRenderer.materials[0]; |
| 38 | + renderer.sharedMesh = sourceRenderer.sharedMesh; |
| 39 | + |
| 40 | + // transfer the bone data |
| 41 | + foreach (var targetBone in renderer.bones) |
| 42 | + { |
| 43 | + foreach (var sourceBone in sourceRenderer.bones) |
| 44 | + { |
| 45 | + if (sourceBone.name == targetBone.name) |
| 46 | + { |
| 47 | + targetBone.position = sourceBone.position; |
| 48 | + targetBone.rotation = sourceBone.rotation; |
| 49 | + targetBone.localScale = sourceBone.localScale; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + // remove the renderer if it's not found in the source avatar |
| 58 | + Object.Destroy(renderer.gameObject); |
17 | 59 | }
|
18 |
| - Mesh mesh = sourceMeshes[i].sharedMesh; |
19 |
| - targetMeshes[i].sharedMesh = mesh; |
20 |
| - targetMeshes[i].enabled = true; |
21 |
| - Material[] materials = sourceMeshes[i].sharedMaterials; |
22 |
| - targetMeshes[i].sharedMaterials = materials; |
23 | 60 | }
|
24 |
| - |
25 |
| - Avatar avatar = sourceAnimator.avatar; |
26 |
| - targetAnimator.avatar = avatar; |
| 61 | + |
| 62 | + // transfer the animation avatar |
| 63 | + var sourceAnimator = source.GetComponentInChildren<Animator>(); |
| 64 | + var targetAnimator = target.GetComponentInChildren<Animator>(); |
| 65 | + targetAnimator.avatar = sourceAnimator.avatar; |
27 | 66 | }
|
28 | 67 | }
|
29 | 68 | }
|
0 commit comments