modular-avatar/Editor/ApplyAnimatorDefaultValuesPass.cs

57 lines
1.9 KiB
C#
Raw Normal View History

#if MA_VRCSDK3_AVATARS
#region
using System;
using System.Collections.Immutable;
using System.Linq;
using nadena.dev.ndmf;
2024-12-16 11:51:11 +08:00
using nadena.dev.ndmf.animator;
using UnityEditor.Animations;
using UnityEngine;
#endregion
namespace nadena.dev.modular_avatar.core.editor
{
internal class ApplyAnimatorDefaultValuesPass : Pass<ApplyAnimatorDefaultValuesPass>
{
protected override void Execute(ndmf.BuildContext context)
{
if (!context.AvatarDescriptor) return;
var values = context.GetState<DefaultValues>()?.InitialValueOverrides
?? ImmutableDictionary<string, float>.Empty;
2024-12-16 11:51:11 +08:00
var asc = context.Extension<AnimatorServicesContext>();
2024-12-16 11:51:11 +08:00
foreach (var controller in asc.ControllerContext.GetAllControllers())
{
var parameters = controller.Parameters;
foreach (var (name, parameter) in parameters)
{
2024-12-16 11:51:11 +08:00
if (!values.TryGetValue(name, out var defaultValue)) continue;
2024-12-16 11:51:11 +08:00
switch (parameter.type)
{
case AnimatorControllerParameterType.Bool:
2024-12-16 11:51:11 +08:00
parameter.defaultBool = defaultValue != 0.0f;
break;
case AnimatorControllerParameterType.Int:
2024-12-16 11:51:11 +08:00
parameter.defaultInt = Mathf.RoundToInt(defaultValue);
break;
case AnimatorControllerParameterType.Float:
2024-12-16 11:51:11 +08:00
parameter.defaultFloat = defaultValue;
break;
default:
continue; // unhandled type, e.g. trigger
}
2024-12-16 11:51:11 +08:00
parameters = parameters.SetItem(name, parameter);
}
2024-12-16 11:51:11 +08:00
controller.Parameters = parameters;
}
}
}
}
#endif