fix: keep position/keep rotation modes not applied in editor

This commit is contained in:
bd_ 2023-01-19 21:42:11 +09:00
parent 231ae585bb
commit 55827868f9
2 changed files with 61 additions and 4 deletions

View File

@ -44,6 +44,52 @@ namespace modular_avatar_tests
private void AssertAttachmentMode(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos, private void AssertAttachmentMode(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos,
bool expectSnapRot) bool expectSnapRot)
{
AssertAttachmentModeAtBuild(attachmentMode, expectSnapPos, expectSnapRot);
AssertAttachmentModeInEditor(attachmentMode, expectSnapPos, expectSnapRot);
}
private void AssertAttachmentModeInEditor(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos,
bool expectSnapRot)
{
// Unset gets converted in the custom inspector; until it is, we don't snap (since we need to know the
// position to heuristically set the snapping mode).
if (attachmentMode == BoneProxyAttachmentMode.Unset) return;
var root = CreateRoot("root");
var bone = CreateChild(root, "bone");
var proxy = CreateChild(root, "proxy");
var boneProxy = proxy.AddComponent<ModularAvatarBoneProxy>();
boneProxy.target = bone.transform;
boneProxy.attachmentMode = attachmentMode;
bone.transform.localPosition = Vector3.one;
bone.transform.localRotation = Quaternion.Euler(123, 45, 6);
boneProxy.Update();
if (expectSnapPos)
{
Assert.LessOrEqual(Vector3.Distance(proxy.transform.position, bone.transform.position), 0.0001f);
}
else
{
Assert.GreaterOrEqual(Vector3.Distance(proxy.transform.position, bone.transform.position), 0.0001f);
}
if (expectSnapRot)
{
Assert.LessOrEqual(Quaternion.Angle(proxy.transform.rotation, bone.transform.rotation), 0.0001f);
}
else
{
Assert.GreaterOrEqual(Quaternion.Angle(proxy.transform.rotation, bone.transform.rotation), 0.0001f);
}
}
private void AssertAttachmentModeAtBuild(BoneProxyAttachmentMode attachmentMode, bool expectSnapPos,
bool expectSnapRot)
{ {
var root = CreateRoot("root"); var root = CreateRoot("root");
var bone = CreateChild(root, "bone"); var bone = CreateChild(root, "bone");

View File

@ -106,14 +106,25 @@ namespace nadena.dev.modular_avatar.core
RuntimeUtil.OnHierarchyChanged -= ClearCache; RuntimeUtil.OnHierarchyChanged -= ClearCache;
} }
private void Update() internal void Update()
{ {
if (!RuntimeUtil.isPlaying && target != null && attachmentMode == BoneProxyAttachmentMode.AsChildAtRoot) if (!RuntimeUtil.isPlaying && target != null)
{ {
var targetTransform = target.transform; var targetTransform = target.transform;
var myTransform = transform; var myTransform = transform;
myTransform.position = targetTransform.position; switch (attachmentMode)
myTransform.rotation = targetTransform.rotation; {
case BoneProxyAttachmentMode.AsChildAtRoot:
myTransform.position = targetTransform.position;
myTransform.rotation = targetTransform.rotation;
break;
case BoneProxyAttachmentMode.AsChildKeepPosition:
myTransform.rotation = targetTransform.rotation;
break;
case BoneProxyAttachmentMode.AsChildKeepRotation:
myTransform.position = targetTransform.position;
break;
}
} }
} }