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

如何在Editor模式下从Box类获取所属Container的Animator引用?

解决方案:Editor阶段让Box访问所属Container的Animator

因为你的需求仅在编辑Container的Inspector面板时生效,可以利用Unity编辑器的API实现,无需给Box添加Container引用字段,以下提供两种可行方案:

方案一:基于选中对象快速实现

这种方式依赖当前选中的对象是Container所在的GameObject,代码简洁直接:

using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class Box
{
    // 你的Box自定义字段

    public List<AnimationClip> GetContainerAnimations()
    {
#if UNITY_EDITOR
        // 获取当前选中的Container组件
        var currentContainer = Selection.activeGameObject?.GetComponent<Container>();
        
        if (currentContainer?.animator != null && currentContainer.animator.runtimeAnimatorController != null)
        {
            return new List<AnimationClip>(currentContainer.animator.runtimeAnimatorController.animationClips);
        }
#endif
        return null;
    }
}

方案二:遍历Inspector窗口精准匹配(推荐)

如果存在多Inspector窗口或选中对象不是目标Container的情况,这种方式会遍历所有Inspector窗口,精准找到当前编辑的Container中包含该Box实例的对象,可靠性更高:

using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class Box
{
    // 你的Box自定义字段

    public List<AnimationClip> GetContainerAnimations()
    {
#if UNITY_EDITOR
        // 遍历所有打开的Inspector窗口
        foreach (var inspectorWindow in Resources.FindObjectsOfTypeAll<InspectorWindow>())
        {
            foreach (var serializedObj in inspectorWindow.serializedObjects)
            {
                // 尝试将Inspector编辑的对象转为Container
                var container = serializedObj.targetObject as Container;
                if (container == null) continue;

                // 获取Container中boxes列表的SerializedProperty
                var boxesProp = serializedObj.FindProperty("boxes");
                if (boxesProp == null || !boxesProp.isArray) continue;

                // 遍历列表中的每个Box,匹配当前实例
                for (int i = 0; i < boxesProp.arraySize; i++)
                {
                    var boxProp = boxesProp.GetArrayElementAtIndex(i);
                    var boxInstance = boxProp.boxedValue;
                    
                    if (boxInstance == this)
                    {
                        // 找到所属Container,返回Animator中的动画剪辑
                        if (container.animator != null && container.animator.runtimeAnimatorController != null)
                        {
                            return new List<AnimationClip>(container.animator.runtimeAnimatorController.animationClips);
                        }
                        break;
                    }
                }
            }
        }
#endif
        return null;
    }
}

配套Container类示例

using UnityEngine;
using System.Collections.Generic;

public class Container : MonoBehaviour
{
    public List<Box> boxes = new List<Box>();
    public Animator animator;
}

注意事项

  • 所有编辑器相关代码都用#if UNITY_EDITOR包裹,避免打包时出现编译错误;
  • 方案二中的"boxes"是Container类中列表字段的名称,如果你修改了字段名,需要同步修改这个字符串;
  • 仅在编辑模式下生效,运行时该方法会返回null,符合你的需求场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 00:40:25