C# Unity 5:在编辑器中从C脚本访问/修改AnimatorController

C# Unity 5:在编辑器中从C脚本访问/修改AnimatorController,c#,unity3d,unity3d-editor,C#,Unity3d,Unity3d Editor,我想编写一个编辑器菜单项,用于访问当前打开的动画控制器并销毁/创建/修改动画过渡 基本上,我需要遍历当前打开的animator controller中的所有动画状态/片段,并根据它们的名称创建具有特定的过渡,并调整所有片段的播放速度 我有以下代码片段: UnityEditorInternal.AnimatorController ac = GetComponent<Animator>().runtimeAnimatorController as UnityEditor

我想编写一个编辑器菜单项,用于访问当前打开的动画控制器并销毁/创建/修改动画过渡

基本上,我需要遍历当前打开的animator controller中的所有动画状态/片段,并根据它们的名称创建具有特定的过渡,并调整所有片段的播放速度

我有以下代码片段:

        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());
                }
            }
        }
然而,它很可能不会在为Unity 4编写的Unity5上编译,我不确定如何使用Unity 5函数获得当前打开的AnimatorController

animator控制器类似乎被定义为UnityEdit.Animations.AnimatorController,但我不知道如何获取当前打开的类


有什么建议吗?

我已经通过inspector上下文菜单访问了AnimatorController。这不是很方便,因为要处理整个控制器,我需要先在项目视图中选择它,但它可以工作

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,我洗耳恭听。

我已通过inspector上下文菜单访问animatorcontroller。这不是很方便,因为要处理整个控制器,我需要先在项目视图中选择它,但它可以工作

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 animator = GetComponent<Animator>();
    string assetPath = AssetDatabase.GetAssetPath(animator.runtimeAnimatorController);
    AnimatorController controller = AssetDatabase.LoadAssetAtPath<AnimatorController>(assetPath);

这对我有用

在打开的Animator窗口中以反射方式获取当前AnimatorController的方法有点老套,它应该被聚焦:

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来查找所选状态相结合,以使我的工具与之配合。

在打开的Animator窗口中以反射方式获取当前AnimatorController的方法有点老套,应该重点关注:

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来查找所选状态相结合,以便使用它制作工具