You need to enable JavaScript to run this app. 如何在Unity Inspector中基于数组长度重命名数组元素
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity Inspector中基于数组长度重命名数组元素

Unity Inspector动态重命名数组元素实现方案

核心思路

Unity原生数组在Inspector默认显示无意义的Element 0、Element 1标签,很容易出现元素拖拽错位的问题。要实现根据数组实际长度自动匹配固定排布规则的元素名称,不需要修改运行时动画逻辑,仅通过Editor拓展即可完成,完全兼容现有的cross product朝向计算逻辑。

实现步骤

1. 定义可复用的标记属性

首先在运行时代码目录下创建自定义PropertyAttribute,用来标记需要动态重命名的数组字段,同时预设固定顺序的腿部命名基准:

using UnityEngine;

/// <summary>
/// 标记需要动态显示名称的腿部数组
/// </summary>
public class LegArrayAttribute : PropertyAttribute
{
    public readonly string[] DefaultElementNames;
    public LegArrayAttribute(params string[] defaultElementNames)
    {
        DefaultElementNames = defaultElementNames;
    }
}

将该属性挂载到你的腿部Transform数组字段上,按照cross product计算要求的固定顺序传入对应名称:

public class ProceduralAnimationEngine : MonoBehaviour
{
    // 按固定顺序传入腿部名称,和数组索引一一对应
    [LegArray("左前腿", "右前腿", "左后腿", "右后腿")]
    public Transform[] legRigElements;
}

2. 编写自定义Inspector绘制逻辑

在项目中任意名为Editor的文件夹下编写对应的PropertyDrawer,拦截数组的默认绘制流程,根据数组当前长度动态替换每个元素的显示标签:

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(LegArrayAttribute))]
public class LegArrayDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var legConfig = (LegArrayAttribute)attribute;
        
        // 绘制数组整体的折叠标题
        EditorGUI.PropertyField(
            new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight),
            property,
            label,
            true
        );

        // 数组展开状态下逐行重绘元素标签
        if (property.isExpanded)
        {
            int arrayCurrentLength = property.arraySize;
            float lineY = position.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
            
            // 跳过数组size输入行的高度偏移
            lineY += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

            for (int i = 0; i < arrayCurrentLength; i++)
            {
                SerializedProperty currentElement = property.GetArrayElementAtIndex(i);
                // 匹配预设名称,超出预设长度的元素自动生成带序号的兜底名称
                string elementDisplayName = i < legConfig.DefaultElementNames.Length
                    ? legConfig.DefaultElementNames[i]
                    : $"腿部元素_{i}";

                // 替换默认的Element X标签
                GUIContent customLabel = new GUIContent(elementDisplayName);
                EditorGUI.PropertyField(
                    new Rect(position.x, lineY, position.width, EditorGUIUtility.singleLineHeight),
                    currentElement,
                    customLabel
                );

                lineY += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
            }
        }
    }

    // 重新计算数组在Inspector中的总高度,避免UI重叠
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        if (!property.isExpanded) return EditorGUIUtility.singleLineHeight;
        int elementCount = property.arraySize;
        return EditorGUIUtility.singleLineHeight * (elementCount + 2) 
               + EditorGUIUtility.standardVerticalSpacing * (elementCount + 1);
    }
}
#endif

注意:上述Drawer脚本必须存放在任意名为Editor的文件夹下,否则会触发打包时的程序集引用错误,导致项目无法构建。

效果适配说明

  • 当数组长度和预设命名长度完全匹配时,每个元素会精准显示对应固定位置的腿部名称,从操作层面避免用户拖拽错位
  • 当数组长度超过预设命名长度时,会自动为超出部分生成带序号的兜底名称,适配不同物种的腿部数量需求(比如8腿蜘蛛、2腿人形角色)
  • 整个绘制流程仅修改Inspector的显示文本,不会改动实际的数组索引顺序,完全不影响基于cross product的身体朝向计算逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.03 23:15:09