From 4ce3223844b5b6decf4cbfdf306bb93c77e9c262 Mon Sep 17 00:00:00 2001 From: bd_ Date: Sat, 19 Nov 2022 19:12:13 -0800 Subject: [PATCH] Add additional bone proxy validation Closes: #85 --- .../Editor/BoneProxyProcessor.cs | 29 ++++++++++++++++++- .../Editor/Inspector/BoneProxyEditor.cs | 13 ++++++++- .../Editor/Localization/en.json | 4 ++- .../Editor/Localization/ja.json | 4 ++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Packages/nadena.dev.modular-avatar/Editor/BoneProxyProcessor.cs b/Packages/nadena.dev.modular-avatar/Editor/BoneProxyProcessor.cs index 8fde4477..f2e4f8c3 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/BoneProxyProcessor.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/BoneProxyProcessor.cs @@ -28,13 +28,20 @@ namespace nadena.dev.modular_avatar.core.editor { internal class BoneProxyProcessor { + internal enum ValidationResult + { + OK, + MovingTarget, + NotInAvatar + } + internal void OnPreprocessAvatar(GameObject avatarGameObject) { var boneProxies = avatarGameObject.GetComponentsInChildren(true); foreach (var proxy in boneProxies) { - if (proxy.target != null) + if (proxy.target != null && ValidateTarget(avatarGameObject, proxy.target) == ValidationResult.OK) { var oldPath = RuntimeUtil.AvatarRootPath(proxy.gameObject); Transform transform = proxy.transform; @@ -51,5 +58,25 @@ namespace nadena.dev.modular_avatar.core.editor Object.DestroyImmediate(proxy); } } + + internal static ValidationResult ValidateTarget(GameObject avatarGameObject, Transform proxyTarget) + { + var avatar = avatarGameObject.transform; + var node = proxyTarget; + + while (node != null && node != avatar) + { + if (node.GetComponent() != null || + node.GetComponent() != null) + { + return ValidationResult.MovingTarget; + } + + node = node.parent; + } + + if (node == null) return ValidationResult.NotInAvatar; + else return ValidationResult.OK; + } } } \ No newline at end of file diff --git a/Packages/nadena.dev.modular-avatar/Editor/Inspector/BoneProxyEditor.cs b/Packages/nadena.dev.modular-avatar/Editor/Inspector/BoneProxyEditor.cs index 7ee882ae..24e089e9 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/Inspector/BoneProxyEditor.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/Inspector/BoneProxyEditor.cs @@ -55,6 +55,15 @@ namespace nadena.dev.modular_avatar.core.editor var virtObj = new SerializedObject(objRefs); var virtProp = virtObj.FindProperty(nameof(TempObjRef.target)); + if (virtProp.objectReferenceValue is Transform targetTransform) + { + var validationResult = BoneProxyProcessor.ValidateTarget(parentAvatar, targetTransform); + if (validationResult != BoneProxyProcessor.ValidationResult.OK) + { + EditorGUILayout.HelpBox(S("boneproxy.err." + validationResult), MessageType.Error); + } + } + EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(virtProp, G("boneproxy.target")); @@ -65,7 +74,9 @@ namespace nadena.dev.modular_avatar.core.editor { var t = (ModularAvatarBoneProxy) targets[i]; Undo.RecordObjects(targets, "Set targets"); - t.target = ((TempObjRef) objRefs[i]).target; + var xform = ((TempObjRef) objRefs[i]).target; + if (RuntimeUtil.FindAvatarInParents(xform)?.gameObject != parentAvatar) continue; + t.target = xform; } } } diff --git a/Packages/nadena.dev.modular-avatar/Editor/Localization/en.json b/Packages/nadena.dev.modular-avatar/Editor/Localization/en.json index d5d4e2f7..1a93919e 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/Localization/en.json +++ b/Packages/nadena.dev.modular-avatar/Editor/Localization/en.json @@ -45,5 +45,7 @@ "blendshape.mesh": "Mesh", "blendshape.source": "Source blendshape", "blendshape.target": "Target blendshape", - "hint.not_in_avatar": "This component needs to be placed inside your avatar to work properly." + "hint.not_in_avatar": "This component needs to be placed inside your avatar to work properly.", + "boneproxy.err.MovingTarget": "You cannot specify a target object that will be moved by other Modular Avatar components", + "boneproxy.err.NotInAvatar": "You must specify an object that is in the avatar" } \ No newline at end of file diff --git a/Packages/nadena.dev.modular-avatar/Editor/Localization/ja.json b/Packages/nadena.dev.modular-avatar/Editor/Localization/ja.json index ddc44db7..d88e4f97 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/Localization/ja.json +++ b/Packages/nadena.dev.modular-avatar/Editor/Localization/ja.json @@ -45,5 +45,7 @@ "blendshape.mesh": "メッシュ", "blendshape.source": "元メッシュのブレンドシェープ", "blendshape.target": "このメッシュのブレンドシェープ", - "hint.not_in_avatar": "このコンポーネントが正しく動作するには、アバター内に配置する必要があります。" + "hint.not_in_avatar": "このコンポーネントが正しく動作するには、アバター内に配置する必要があります。", + "boneproxy.err.MovingTarget": "他のモジュラーアバターコンポーネントで移動されるオブジェクトを指定できません。", + "boneproxy.err.NotInAvatar": "アバター内のオブジェクトを指定してください。" } \ No newline at end of file