using System.Collections.Generic; using System.Linq; using nadena.dev.ndmf; using UnityEditor.EditorTools; using UnityEngine; namespace nadena.dev.modular_avatar.core.editor { internal class ScaleAdjusterPass : Pass { protected override void Execute(ndmf.BuildContext context) { Dictionary boneMappings = new Dictionary(); foreach (var component in context.AvatarRootObject.GetComponentsInChildren()) { var proxyTransform = component.transform; var parentAdjuster = component.transform.parent?.GetComponent(); if (parentAdjuster != null) { UnityEngine.Object.DestroyImmediate(component); proxyTransform.localScale = parentAdjuster.Scale; parentAdjuster.scaleProxy = null; // prevent destruction of the ScaleProxy itself UnityEngine.Object.DestroyImmediate(parentAdjuster); boneMappings.Add(proxyTransform.parent, proxyTransform); } } foreach (var sar in context.AvatarRootObject.GetComponentsInChildren()) { UnityEngine.Object.DestroyImmediate(sar.gameObject); } if (boneMappings.Count == 0) { return; } foreach (var smr in context.AvatarRootObject.GetComponentsInChildren()) { var bones = smr.bones; for (int i = 0; i < bones.Length; i++) { if (bones[i] != null && boneMappings.TryGetValue(bones[i], out var newBone)) { bones[i] = newBone; } } smr.bones = bones; } } } }