diff --git a/Runtime/Core/Scripts/Data/AvatarData.cs b/Runtime/Core/Scripts/Data/AvatarData.cs index 89bc28ae..ad371f9f 100644 --- a/Runtime/Core/Scripts/Data/AvatarData.cs +++ b/Runtime/Core/Scripts/Data/AvatarData.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System; +using UnityEngine; namespace ReadyPlayerMe.Core { @@ -6,5 +7,37 @@ public class AvatarData : MonoBehaviour { public string AvatarId; public AvatarMetadata AvatarMetadata; + + private SkinnedMeshRenderer[] meshes; + + private void Awake() + { + meshes = GetComponentsInChildren(); + } + + private void OnDestroy() + { + foreach (var mesh in meshes) + { + var materials = mesh.sharedMaterials; + + foreach (var material in materials) + { + if(material == null) continue; + + foreach (var property in material.GetTexturePropertyNames()) + { + Texture texture = material.GetTexture(property); + + if(texture == null) continue; + + Destroy(texture); + } + Destroy(material); + } + + Destroy(mesh.sharedMesh); + } + } } }