我想编写一个编辑器菜单项来访问当前打开的动画 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/