modular-avatar/Editor/ReactiveObjects/MenuItemPreviewCondition.cs
Haï~ 3b067e4664
Make compatible with Unity 6 projects (#1232)
* Disable compilation for use in Unity 6 (6000.0.20f1):
- Do not compile some classes and code paths in non-VRChat projects.
- This has been tested in Unity 6 (6000.0.20f1).

* Fix hide internal components in Unity 6:
- [AddComponentMenu("")] does not work in Unity 6.
- Replace it with [AddComponentMenu("/")]
- This alternative is confirmed to also work in Unity 2022.

---------

Co-authored-by: Haï~ <hai-vr@users.noreply.github.com>
Co-authored-by: bd_ <bd_@nadena.dev>
2024-10-19 18:58:41 -07:00

75 lines
2.7 KiB
C#

#if MA_VRCSDK3_AVATARS
using System;
using System.Collections.Generic;
using nadena.dev.ndmf;
using nadena.dev.ndmf.preview;
using UnityEngine;
namespace nadena.dev.modular_avatar.core.editor
{
internal class MenuItemPreviewCondition
{
private readonly ComputeContext _context;
private readonly ParameterInfo _info;
// avatar root => params
private readonly Dictionary<GameObject, Dictionary<string, ProvidedParameter>> _registeredParameters = new();
public MenuItemPreviewCondition(ComputeContext computeContext)
{
if (computeContext == null) throw new ArgumentNullException(nameof(computeContext));
_info = ParameterInfo.ForPreview(computeContext);
_context = computeContext;
}
private Dictionary<string, ProvidedParameter> RegisteredParameters(GameObject obj)
{
_context.ObservePath(obj.transform);
var root = RuntimeUtil.FindAvatarInParents(obj.transform)?.gameObject;
if (root == null) return new Dictionary<string, ProvidedParameter>();
if (_registeredParameters.TryGetValue(root, out var parameters))
return parameters;
parameters = new Dictionary<string, ProvidedParameter>();
foreach (var param in _info.GetParametersForObject(root)) parameters[param.EffectiveName] = param;
_registeredParameters[root] = parameters;
return parameters;
}
private bool TryGetRegisteredParam(ModularAvatarMenuItem mami, string paramName,
out ProvidedParameter providedParameter)
{
providedParameter = default;
if (string.IsNullOrWhiteSpace(mami.Control?.parameter?.name)) return false;
var remaps = _info.GetParameterRemappingsAt(mami.gameObject);
if (remaps.TryGetValue((ParameterNamespace.Animator, paramName), out var remap))
paramName = remap.ParameterName;
return RegisteredParameters(mami.gameObject).TryGetValue(paramName, out providedParameter);
}
public bool IsEnabledForPreview(ModularAvatarMenuItem mami)
{
_context.ObservePath(mami.transform);
if (_context.Observe(mami, _ => mami.Control == null)) return false;
var (paramName, value) = _context.Observe(mami, m => (m.Control.parameter.name, m.Control.value));
if (TryGetRegisteredParam(mami, paramName, out var providedParameter))
{
var defaultValue = providedParameter.DefaultValue ?? 0;
return Mathf.Abs(defaultValue - value) < 0.01f;
}
return _context.Observe(mami, _ => mami.isDefault);
}
}
}
#endif