c# - 统一 5 : access/modify AnimatorController from C# script in editor

标签 c# unity3d unity3d-editor

我想编写一个编辑器菜单项来访问当前打开的动画 Controller 并销毁/创建/修改动画过渡。

基本上,我需要遍历当前打开的动画 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());
                }
            }
        }

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

动画 Controller 类似乎定义为 UnityEditor.Animations.AnimatorController,但我不知道如何获取当前打开的 Controller 。

有什么建议吗?

最佳答案

我已经设法通过检查器上下文菜单访问 AnimatorController。这不是很方便(因为要处理整个 Controller ,我需要先在项目 View 中选择它),但它可以工作。

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

关于c# - 统一 5 : access/modify AnimatorController from C# script in editor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315507/

相关文章:

c# - Unity - 仅将 -1 和 1 设为要放置在检查器中的变量选项

android - 如何使用unity3d在移动设备上正确加载XML文件

c# - 更改实例化对象的父级

c# - 如何统一平滑地旋转相机?

c# - 将 IList<T1> 转换为 ILIst<T2>

c# - 删除和创建新的/覆盖同名的文件 c# .NET

unity3d - 编辑器窗口截图

unity3d - Unity3d Undo是一个EditorWindow [用于 bool 和字符串]

c# - 正则表达式捕获具有 1 到 5 个序数的组

android - 使用顶部的另一个 Activity 恢复 Unity3d Android 应用程序