fix: BoneProxy non-humanoid references did not save properly

This commit is contained in:
bd_ 2023-01-18 19:08:07 +09:00
parent be32ed55f5
commit 33895a58e8
3 changed files with 35 additions and 15 deletions

View File

@ -21,6 +21,27 @@ namespace modular_avatar_tests
expectSnapRot: false); expectSnapRot: false);
} }
[Test]
public void TestNonHumanoidTarget()
{
var root = CreateRoot("root");
var target = CreateChild(root, "target");
var reference = CreateChild(root, "ref");
var boneProxy = reference.AddComponent<ModularAvatarBoneProxy>();
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, private void AssertAttachmentMode(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos,
bool expectSnapRot) bool expectSnapRot)
{ {

View File

@ -71,7 +71,7 @@ namespace nadena.dev.modular_avatar.core
get get
{ {
if (_targetCache != null) return _targetCache; if (_targetCache != null) return _targetCache;
UpdateDynamicMapping(); _targetCache = UpdateDynamicMapping();
RuntimeUtil.OnHierarchyChanged -= ClearCache; RuntimeUtil.OnHierarchyChanged -= ClearCache;
RuntimeUtil.OnHierarchyChanged += ClearCache; RuntimeUtil.OnHierarchyChanged += ClearCache;
return _targetCache; return _targetCache;
@ -100,7 +100,7 @@ namespace nadena.dev.modular_avatar.core
ClearCache(); ClearCache();
} }
void ClearCache() internal void ClearCache()
{ {
_targetCache = null; _targetCache = null;
RuntimeUtil.OnHierarchyChanged -= ClearCache; RuntimeUtil.OnHierarchyChanged -= ClearCache;
@ -122,34 +122,32 @@ namespace nadena.dev.modular_avatar.core
RuntimeUtil.OnHierarchyChanged -= ClearCache; 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); var avatar = RuntimeUtil.FindAvatarInParents(transform);
if (avatar == null) return; if (avatar == null) return null;
if (subPath == "$$AVATAR") if (subPath == "$$AVATAR")
{ {
target = avatar.transform; return avatar.transform;
return;
} }
if (boneReference == HumanBodyBones.LastBone) if (boneReference == HumanBodyBones.LastBone)
{ {
target = avatar.transform.Find(subPath); return avatar.transform.Find(subPath);
return;
} }
var animator = avatar.GetComponent<Animator>(); var animator = avatar.GetComponent<Animator>();
if (animator == null) return; if (animator == null) return null;
var bone = animator.GetBoneTransform(boneReference); var bone = animator.GetBoneTransform(boneReference);
if (bone == null) return; if (bone == null) return null;
if (string.IsNullOrWhiteSpace(subPath)) _targetCache = bone; if (string.IsNullOrWhiteSpace(subPath)) return bone;
else _targetCache = bone.Find(subPath); else return bone.Find(subPath);
} }
private void UpdateStaticMapping(Transform newTarget) private void UpdateStaticMapping(Transform newTarget)

View File

@ -1,3 +1,4 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("nadena.dev.modular-avatar.core.editor")] [assembly: InternalsVisibleTo("nadena.dev.modular-avatar.core.editor")]
[assembly: InternalsVisibleTo("Tests")]