From d4683f99e3dc6f380ffce3d033624d03024074b6 Mon Sep 17 00:00:00 2001 From: bd_ Date: Thu, 8 Aug 2024 21:51:56 -0700 Subject: [PATCH] feat: add parameter introspection for automatic parameters (#970) --- .../ParamsUsage/MAParametersIntrospection.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Editor/ParamsUsage/MAParametersIntrospection.cs b/Editor/ParamsUsage/MAParametersIntrospection.cs index a5500df7..cfc3dfe5 100644 --- a/Editor/ParamsUsage/MAParametersIntrospection.cs +++ b/Editor/ParamsUsage/MAParametersIntrospection.cs @@ -11,6 +11,49 @@ using UnityEngine; namespace nadena.dev.modular_avatar.core.editor { + [ParameterProviderFor(typeof(ModularAvatarMenuItem))] + internal class MAMenuItemIntrospection : IParameterProvider + { + private readonly ModularAvatarMenuItem _component; + + public MAMenuItemIntrospection(ModularAvatarMenuItem menuItem) + { + _component = menuItem; + } + + public IEnumerable GetSuppliedParameters(ndmf.BuildContext context = null) + { + if (_component.Control == null || _component.GetComponent() == null) yield break; + + var hidden = false; + var name = _component.Control?.parameter?.name; + if (string.IsNullOrWhiteSpace(name)) + { + name = $"__MA/AutoParam/{_component.gameObject.name}${_component.GetInstanceID()}"; + hidden = true; + } + + var type = AnimatorControllerParameterType.Bool; + + if (type != AnimatorControllerParameterType.Float && + (_component.Control.value > 1.01 || _component.Control.value < -0.01)) + type = AnimatorControllerParameterType.Int; + + if (Mathf.Abs(Mathf.Round(_component.Control.value) - _component.Control.value) > 0.01f) + type = AnimatorControllerParameterType.Float; + + yield return new ProvidedParameter( + name, + ParameterNamespace.Animator, + _component, PluginDefinition.Instance, type) + { + WantSynced = _component.isSynced, + IsHidden = hidden, + DefaultValue = _component.isDefault ? _component.Control.value : null + }; + } + } + [ParameterProviderFor(typeof(ModularAvatarParameters))] internal class MAParametersIntrospection : IParameterProvider {