Unity 5:access/modify 来自编辑器中 C# 脚本的 AnimatorController

Unity 5: access/modify AnimatorController from C# script in editor

我想编写一个访问当前打开的动画控制器和 destroys/creates/modifies 动画过渡的编辑器菜单项。

基本上,我需要遍历当前打开的动画控制器中的所有动画 states/clips,并根据它们的名称创建特定的过渡并调整所有剪辑的播放速度。

我有这个代码片段:

        UnityEditorInternal.AnimatorController ac = GetComponent<Animator>().runtimeAnimatorController as UnityEditorInternal.AnimatorController;
        int numLayers = ac.layerCount;

        for(int i = 0; i<numLayers; i++){
            UnityEditorInternal.AnimatorControllerLayer layer = ac.GetLayer(i);
            Debug.Log ("Layer " + i + " is: " + layer.name + " and has " + layer.stateMachine.stateCount + " states");
            UnityEditorInternal.StateMachine sm = layer.stateMachine;
            for(int n = 0; n<sm.stateCount; n++){
                UnityEditorInternal.State state = sm.GetState(n);
                Debug.Log ("State " + state.name + " is " + state.GetHashCode());
                UnityEditorInternal.Transition[] list = sm.GetTransitionsFromState(state);
                for(int j = 0; j<list.Length; j++){
                    UnityEditorInternal.Transition transition = list[j];
                    Debug.Log ("Transition: " + transition.name + " is " + transition.GetHashCode());
                }
            }
        }

但是,它无法在 Unity5 上编译(很可能是为 Unity 4 编写的),而且我不确定如何使用 Unity 5 函数获取当前打开的 AnimatorController。

动画器控制器class 似乎定义为UnityEditor.Animations.AnimatorController,但我不知道如何抓取当前打开的控制器。

有什么建议吗?

我已经设法通过检查器上下文菜单访问 AnimatorController。这不是很方便(因为要处理整个控制器,我需要先在项目视图中select),但它确实有效。

public class AnimImportTools: MonoBehaviour{
//.....

    [MenuItem("CONTEXT/AnimatorController/Make transitions immediate")]
    private static void makeTransitionsImmediate(){
        UnityEditor.Animations.AnimatorController ac = Selection.activeObject as UnityEditor.Animations.AnimatorController;
        foreach(var layer in ac.layers){
            foreach(var curState in layer.stateMachine.states){
                foreach(var transition in curState.state.transitions){
                    transition.duration = 0.0f;
                    transition.exitTime = 1.0f;
                }
            }
        }
    }
//.....
}

如果有人知道更好的方法 - 即在更容易访问的位置添加菜单或 运行 来自主菜单的脚本(并从那里获取当前打开的 animatorcontroller),我洗耳恭听。

感谢您的回答,我得出以下结论:

    Animator animator = GetComponent<Animator>();
    string assetPath = AssetDatabase.GetAssetPath(animator.runtimeAnimatorController);
    AnimatorController controller = AssetDatabase.LoadAssetAtPath<AnimatorController>(assetPath);

这对我有用!

在打开的 Animator 中以反射方式获取当前 AnimatorController 的方式有点笨拙 window(应该重点关注):

static AnimatorController GetCurrentController()
{
    AnimatorController controller = null;
    var tool = EditorWindow.focusedWindow;
    var toolType = tool.GetType();
    var controllerProperty = toolType.GetProperty("animatorController", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    if (controllerProperty != null)
    {
        controller = controllerProperty.GetValue(tool, null) as AnimatorController;
    }
    else
    {
        Debug.Log("EditorWindow.focusedWindow " + tool + " does not contain animatorController",tool);
    }
    return controller;
}

适用于我当前的 Unity 版本 (5.2.2p4)。

然后我将其与使用 Selection.objects 相结合以找到选定的状态来制作我的工具。