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);
}
[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,
bool expectSnapRot)
{

View File

@ -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<Animator>();
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)

View File

@ -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")]