modular-avatar/Editor/ScaleAdjusterPass.cs
bd_ d26ac60be5
fix: build fails when entries in the bones array are null (#682)
* fix: build fails when entries in the bones array are null

* chore: skip bone remapping when no Scale Adjusters are in use
2024-02-19 17:27:36 +09:00

56 lines
2.0 KiB
C#

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<ScaleAdjusterPass>
{
protected override void Execute(ndmf.BuildContext context)
{
ScaleAdjusterRenderer.ClearAllOverrides();
Dictionary<Transform, Transform> boneMappings = new Dictionary<Transform, Transform>();
foreach (var component in context.AvatarRootObject.GetComponentsInChildren<ScaleProxy>())
{
var proxyTransform = component.transform;
var parentAdjuster = component.transform.parent?.GetComponent<ModularAvatarScaleAdjuster>();
if (parentAdjuster != null)
{
UnityEngine.Object.DestroyImmediate(component);
proxyTransform.localScale = parentAdjuster.Scale;
UnityEngine.Object.DestroyImmediate(parentAdjuster);
boneMappings.Add(proxyTransform.parent, proxyTransform);
}
}
foreach (var sar in context.AvatarRootObject.GetComponentsInChildren<ScaleAdjusterRenderer>())
{
UnityEngine.Object.DestroyImmediate(sar.gameObject);
}
if (boneMappings.Count == 0)
{
return;
}
foreach (var smr in context.AvatarRootObject.GetComponentsInChildren<SkinnedMeshRenderer>())
{
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;
}
}
}
}