You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C#如何获取Animator组件内部参数的列表、数组及参数数量?

Unity Animator 布尔参数批量查询实现方案

我们可以直接通过Unity官方提供的Animator参数API实现批量查询,完全不需要额外依赖,核心逻辑是遍历Animator的所有参数,筛选布尔类型后批量取值,还可以针对高频判断场景做性能优化。

核心实现思路

  • 调用Animator.parameters或者Animator.GetParameter(i)获取当前Animator的所有配置参数
  • 遍历过程中通过parameter.type == AnimatorControllerParameterType.Bool筛选出布尔类型参数
  • 优先用nameHash代替字符串参数名取值,避免字符串查找的性能开销
  • 针对高频判断「所有布尔参数是否全为false」的场景,可以提前终止遍历,不需要存储所有参数值

通用扩展方法实现

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Animator布尔参数批量查询扩展
/// </summary>
public static class AnimatorBoolQueryExtension
{
    /// <summary>
    /// 获取当前Animator所有布尔参数的键值对
    /// </summary>
    public static Dictionary<string, bool> GetAllBoolStates(this Animator animator)
    {
        Dictionary<string, bool> boolStates = new Dictionary<string, bool>();
        for (int i = 0; i < animator.parameterCount; i++)
        {
            var param = animator.GetParameter(i);
            if (param.type == AnimatorControllerParameterType.Bool)
            {
                boolStates.Add(param.name, animator.GetBool(param.nameHash));
            }
        }
        return boolStates;
    }

    /// <summary>
    /// 直接判断所有布尔参数是否全部为false,性能远高于先取值再判断
    /// </summary>
    public static bool IsAllBoolFalse(this Animator animator)
    {
        for (int i = 0; i < animator.parameterCount; i++)
        {
            var param = animator.GetParameter(i);
            if (param.type == AnimatorControllerParameterType.Bool && animator.GetBool(param.nameHash))
            {
                // 只要存在一个为true的布尔参数就直接返回
                return false;
            }
        }
        return true;
    }
}

基础使用示例

// 你的业务代码中直接调用扩展方法即可
Animator targetAnim = GetComponent<Animator>();

// 场景1:快速判断所有布尔参数是否全为false
if (targetAnim.IsAllBoolFalse())
{
    // 执行你需要的逻辑
}

// 场景2:需要获取所有布尔参数做复杂判断
var allBoolStates = targetAnim.GetAllBoolStates();
foreach (var state in allBoolStates)
{
    Debug.Log($"参数名:{state.Key},当前值:{state.Value}");
}

高频调用场景优化方案

如果你的参数列表运行时不会修改,可以提前缓存布尔参数的nameHash,避免每次调用都重复筛选参数类型,性能可以提升30%以上:

// 写在你的业务类中
private Animator _anim;
private int[] _cachedBoolParamHashes;

private void Awake()
{
    _anim = GetComponent<Animator>();
    // 初始化阶段缓存所有布尔参数的hash
    List<int> hashList = new List<int>();
    for (int i = 0; i < _anim.parameterCount; i++)
    {
        var param = _anim.GetParameter(i);
        if (param.type == AnimatorControllerParameterType.Bool)
        {
            hashList.Add(param.nameHash);
        }
    }
    _cachedBoolParamHashes = hashList.ToArray();
}

// 后续判断直接用缓存的hash数组遍历,无额外筛选开销
public bool CustomCheckAllBoolFalse()
{
    for (int i = 0; i < _cachedBoolParamHashes.Length; i++)
    {
        if (_anim.GetBool(_cachedBoolParamHashes[i]))
        {
            return false;
        }
    }
    return true;
}

内容的提问来源于stack exchange,提问作者TheWIzard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 11:45:08