From 33895a58e8190c53ec11a7dddeda8383d9b9e2b5 Mon Sep 17 00:00:00 2001 From: bd_ Date: Wed, 18 Jan 2023 19:08:07 +0900 Subject: [PATCH] fix: BoneProxy non-humanoid references did not save properly --- .../EditModeTests/BoneProxyTest.cs | 21 +++++++++++++++ .../Runtime/ModularAvatarBoneProxy.cs | 26 +++++++++---------- .../Runtime/assembly-info.cs | 3 ++- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs b/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs index d97e4729..d09852c2 100644 --- a/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs +++ b/Assets/_ModularAvatar/EditModeTests/BoneProxyTest.cs @@ -21,6 +21,27 @@ namespace modular_avatar_tests expectSnapRot: false); } + [Test] + public void TestNonHumanoidTarget() + { + var root = CreateRoot("root"); + var target = CreateChild(root, "target"); + var reference = CreateChild(root, "ref"); + + var boneProxy = reference.AddComponent(); + boneProxy.target = root.transform; + boneProxy.ClearCache(); + Assert.AreEqual(root.transform, boneProxy.target); + + boneProxy.target = target.transform; + boneProxy.ClearCache(); + Assert.AreEqual(target.transform, boneProxy.target); + + target.name = "target2"; + boneProxy.ClearCache(); + Assert.IsNull(boneProxy.target); + } + private void AssertAttachmentMode(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos, bool expectSnapRot) { diff --git a/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs b/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs index 8da0ef57..f9c4d339 100644 --- a/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs +++ b/Packages/nadena.dev.modular-avatar/Runtime/ModularAvatarBoneProxy.cs @@ -71,7 +71,7 @@ namespace nadena.dev.modular_avatar.core get { if (_targetCache != null) return _targetCache; - UpdateDynamicMapping(); + _targetCache = UpdateDynamicMapping(); RuntimeUtil.OnHierarchyChanged -= ClearCache; RuntimeUtil.OnHierarchyChanged += ClearCache; return _targetCache; @@ -100,7 +100,7 @@ namespace nadena.dev.modular_avatar.core ClearCache(); } - void ClearCache() + internal void ClearCache() { _targetCache = null; RuntimeUtil.OnHierarchyChanged -= ClearCache; @@ -122,34 +122,32 @@ namespace nadena.dev.modular_avatar.core RuntimeUtil.OnHierarchyChanged -= ClearCache; } - private void UpdateDynamicMapping() + private Transform UpdateDynamicMapping() { - if (boneReference == HumanBodyBones.LastBone) + if (boneReference == HumanBodyBones.LastBone && string.IsNullOrWhiteSpace(subPath)) { - return; + return null; } var avatar = RuntimeUtil.FindAvatarInParents(transform); - if (avatar == null) return; + if (avatar == null) return null; if (subPath == "$$AVATAR") { - target = avatar.transform; - return; + return avatar.transform; } if (boneReference == HumanBodyBones.LastBone) { - target = avatar.transform.Find(subPath); - return; + return avatar.transform.Find(subPath); } var animator = avatar.GetComponent(); - if (animator == null) return; + if (animator == null) return null; var bone = animator.GetBoneTransform(boneReference); - if (bone == null) return; - if (string.IsNullOrWhiteSpace(subPath)) _targetCache = bone; - else _targetCache = bone.Find(subPath); + if (bone == null) return null; + if (string.IsNullOrWhiteSpace(subPath)) return bone; + else return bone.Find(subPath); } private void UpdateStaticMapping(Transform newTarget) diff --git a/Packages/nadena.dev.modular-avatar/Runtime/assembly-info.cs b/Packages/nadena.dev.modular-avatar/Runtime/assembly-info.cs index 77916945..9e586b59 100644 --- a/Packages/nadena.dev.modular-avatar/Runtime/assembly-info.cs +++ b/Packages/nadena.dev.modular-avatar/Runtime/assembly-info.cs @@ -1,3 +1,4 @@ using System.Runtime.CompilerServices; -[assembly:InternalsVisibleTo("nadena.dev.modular-avatar.core.editor")] +[assembly: InternalsVisibleTo("nadena.dev.modular-avatar.core.editor")] +[assembly: InternalsVisibleTo("Tests")] \ No newline at end of file