From 7ca52e5a0a151f0674f6fe2dfbbf3b42a53306b9 Mon Sep 17 00:00:00 2001 From: bd_ Date: Mon, 3 Oct 2022 18:29:49 -0700 Subject: [PATCH] [MergeArmature] Restore support for inferring prefix/suffix --- .../Editor/Inspector/MergeArmatureEditor.cs | 56 +++++++++++++++++++ .../Inspector/MergeArmatureEditor.cs.meta | 3 + 2 files changed, 59 insertions(+) create mode 100644 Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs create mode 100644 Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs.meta diff --git a/Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs b/Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs new file mode 100644 index 00000000..3f143917 --- /dev/null +++ b/Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs @@ -0,0 +1,56 @@ +using System; +using UnityEditor; +using UnityEngine; + +namespace net.fushizen.modular_avatar.core.editor +{ + [CustomEditor(typeof(ModularAvatarMergeArmature))] + public class MergeArmatureEditor : Editor + { + public override void OnInspectorGUI() + { + var target = (ModularAvatarMergeArmature) this.target; + var priorMergeTarget = target.mergeTargetObject; + + EditorGUI.BeginChangeCheck(); + base.OnInspectorGUI(); + if (EditorGUI.EndChangeCheck()) + { + serializedObject.ApplyModifiedProperties(); + + if (target.mergeTargetObject != null && priorMergeTarget == null + && string.IsNullOrEmpty(target.prefix) + && string.IsNullOrEmpty(target.suffix)) + { + // We only infer if targeting the armature (below the Hips bone) + var rootAnimator = RuntimeUtil.FindAvatarInParents(target.transform)?.GetComponent(); + if (rootAnimator == null) return; + + var hips = rootAnimator.GetBoneTransform(HumanBodyBones.Hips); + if (hips == null || hips.transform.parent != target.mergeTargetObject.transform) return; + + // We also require that the attached object has exactly one child (presumably the hips) + if (target.transform.childCount != 1) return; + + // Infer the prefix and suffix by comparing the names of the mergeTargetObject's hips with the child of the + // GameObject we're attached to. + var baseName = hips.name; + var mergeName = target.transform.GetChild(0).name; + + var prefixLength = mergeName.IndexOf(baseName, StringComparison.InvariantCulture); + if (prefixLength < 0) return; + + var suffixLength = mergeName.Length - prefixLength - baseName.Length; + + target.prefix = mergeName.Substring(0, prefixLength); + target.suffix = mergeName.Substring(mergeName.Length - suffixLength); + + if (!string.IsNullOrEmpty(target.prefix) || !string.IsNullOrEmpty(target.suffix)) + { + RuntimeUtil.MarkDirty(target); + } + } + } + } + } +} \ No newline at end of file diff --git a/Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs.meta b/Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs.meta new file mode 100644 index 00000000..57774bf0 --- /dev/null +++ b/Packages/net.fushizen.modular-avatar/Editor/Inspector/MergeArmatureEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f8a400eb7fa8474fb04770eca10b1255 +timeCreated: 1664846740 \ No newline at end of file