using UnityEngine;  
using System.Collections;  
using System.Collections.Generic;  
using UnityEditor;  
using System.IO;  
using System.Reflection;  
using UnityEditorInternal;    

public class AnimatorHelper{  
    
    [MenuItem("Tools/程序/AnimatorHelper/RemoveTransitions")]
    static void RemoveTransitions()  {  
        Debug.LogError("**********************RemoveTransitions Begin************************");  
        UnityEngine.Object[] selections = Selection.GetFiltered(typeof(AnimatorController), SelectionMode.Assets);  
        foreach (Object obj in selections)  {             
            string animatorPath = AssetDatabase.GetAssetPath(obj.GetInstanceID());  
            Debug.LogError("Process Animator... path: " + animatorPath);  
            RemoveTransition(animatorPath);  
        }  
        Debug.LogError("**********************RemoveTransitions End***************************");  
    }  
    
    static void RemoveTransition(string _animatorPath)  {  
        string newanimatorpath = GetNewAnimatorPath(_animatorPath); //"Assets/Resources/AnimatorCtrl/aaaaa.controller";  
        Debug.LogError("New Converted AnimatorController: " + newanimatorpath);       
        if (System.IO.File.Exists(newanimatorpath))  {          
            AssetDatabase.DeleteAsset(newanimatorpath);  
        }
        AssetDatabase.CopyAsset(_animatorPath, newanimatorpath);  
        AssetDatabase.Refresh();  
        UnityEditorInternal.AnimatorController ac = Resources.LoadAssetAtPath(newanimatorpath, typeof(UnityEditorInternal.AnimatorController)) as UnityEditorInternal.AnimatorController;  
        if (ac == null)  {  
            Debug.LogError("Load AnimatorController Failed. Path: " + _animatorPath);  
            return;  
        }  
        
        //Remove all the parameters  
        for (int i = ac.parameterCount - 1; i >= 0; --i)  {  
            ac.RemoveParameter(i);  
        }  
        //Remove all the transitions  
        //Add Transition from Any to Base.Run  
        if (ac.layerCount > 1)  {  
            Debug.LogError("Warning Warning Warning. The Animator has more than 1 layer. layerCount: " + ac.layerCount + " Path: " + _animatorPath);  
        }  
        AnimatorControllerLayer layer = ac.GetLayer(0);  
        if (layer != null)  {  
            StateMachine machine = layer.stateMachine;  
            if (machine != null)  {  
                RemoveTransitions(machine);  
                //find "Run" State and Add Transition from Any to Run  
                State stateRun = null;  
                for (int i=0; i<machine.stateCount; ++i)  {                          
                    if (machine.GetState(i).uniqueName == "Base.Run")  {  
                        stateRun = machine.GetState(i);  
                        break;  
                    }  
                }
                machine.AddTransition(null, stateRun);    
            }  
        }  
        else  {  
            Debug.LogError("Get AnimatorControllerLyaer Failed. path: " + _animatorPath);  
        }  
        AssetDatabase.SaveAssets();  
        AssetDatabase.Refresh();  
    }  
    
    static void RemoveTransitions(StateMachine _machine)  {  
        if (_machine != null)  {      
            for (int j = _machine.stateCount - 1; j >= 0; --j)  {  
                RemoveTransitions(_machine, _machine.GetState(j));  
            }  
            RemoveTransitions(_machine, null);    //remove Transition of the Any State  
            for (int j = _machine.stateMachineCount - 1; j >= 0; --j)  {  
                StateMachine subMachine = _machine.GetStateMachine(j);             
                RemoveTransitions(subMachine);    //Recursively  
            }  
        }  
    }  
    
    static void RemoveTransitions(StateMachine _machine, State _state) {  //when _state is null, it means the Any State    
        Transition[] list = _machine.GetTransitionsFromState(_state);     
        for (int i = list.Length - 1; i >= 0; --i)  {     
            _machine.RemoveTransition(list[i]);
        }  
    }
    
    static string GetNewAnimatorPath(string _assetPath)  {  
        if (System.IO.Path.GetExtension(_assetPath) != ".controller")  {  
            Debug.LogError("Error Error Error. asset is not controller. AssetPath: " + _assetPath);  
            return "";  
        }
        string newPath = "";
        string strDir = System.IO.Path.GetDirectoryName(_assetPath);  
        string strfileName = System.IO.Path.GetFileNameWithoutExtension(_assetPath);  
        newPath = strDir + "/" + strfileName + "_lonelyIsland" + System.IO.Path.GetExtension(_assetPath);  
        return newPath;  
    }  
}  

获取控制器并进行修改

 posted on 2016-08-03 16:44  1039781968  阅读(102)  评论(0)    收藏  举报