Unity中使用AssetDatabase.AddObjectToAsset将AnimationClip添加为AnimatorController的子节点

QQ20160719161947
目的是为了达到图中效果。这样的好处是AnimationClip便是相对引用。在复制AnimatorController的时候,不用重新把所有的AnimationClip再关联一遍。
代码如下,由于AnimationClip在作为子节点的时候无法通过原方法进行重命名和删除。所以除了创建动画文件的功能外,还加入了重命名和删除的功能。

public class AnimationHelper : EditorWindow
{
    private enum WindowsType
    {
        Create,
        Rename
    }

    private static WindowsType k_WindowsType;

    [MenuItem("Assets/Animation/CreateSubAnimationClip")]
    private static void CreateSubAnimationClip()
    {
        k_WindowsType = WindowsType.Create;

        Rect wr = new Rect(0, 0, 300, 148);
        AnimationHelper window = (AnimationHelper)EditorWindow.GetWindowWithRect(typeof(AnimationHelper), wr, true, "CreateSubAnimationClip");

        Object[] objs = Selection.objects;
        if (objs.Length == 1 && objs[0] is RuntimeAnimatorController) window.m_Controller = (RuntimeAnimatorController)objs[0];

        window.Show();
    }

    [MenuItem("Assets/Animation/RenameSubAnimationClip")]
    private static void RenameSubAnimationClip()
    {
        k_WindowsType = WindowsType.Rename;

        Rect wr = new Rect(0, 0, 300, 148);
        AnimationHelper window = (AnimationHelper)EditorWindow.GetWindowWithRect(typeof(AnimationHelper), wr, true, "RenameSubAnimationClip");

        Object[] objs = Selection.objects;
        if (objs.Length == 1 && objs[0] is AnimationClip)
        {
            window.m_Clip = (AnimationClip)objs[0];
            window.m_AnimClipName = objs[0].name;
        }

        window.Show();
    }

    [MenuItem("Assets/Animation/DeleteSubAnimationClip")]
    private static void DeleteSubAnimationClip()
    {
        Object[] objs = Selection.objects;
        if (objs.Length == 1 && objs[0] is AnimationClip)
        {
            GameObject.DestroyImmediate(objs[0], true);
            AssetDatabase.SaveAssets();
        }
    }

    private string m_AnimClipName;
    private RuntimeAnimatorController m_Controller;
    private AnimationClip m_Clip;

    private void OnGUI()
    {
        switch (k_WindowsType)
        {
            case WindowsType.Create:
                m_Controller = EditorGUILayout.ObjectField("Animator Controller", m_Controller, typeof(RuntimeAnimatorController), true) as RuntimeAnimatorController;
                m_AnimClipName = EditorGUILayout.TextField("Animation Clip Name", m_AnimClipName);
                if (GUILayout.Button("Create", GUILayout.Height(35)))
                {
                    AnimationClip animationClip = new AnimationClip();
                    animationClip.name = m_AnimClipName;
                    AssetDatabase.AddObjectToAsset(animationClip, m_Controller);
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animationClip));
                }
                break;
            case WindowsType.Rename:
                m_Clip = EditorGUILayout.ObjectField("Animator Clip", m_Clip, typeof(AnimationClip), true) as AnimationClip;
                m_AnimClipName = EditorGUILayout.TextField("Animation Clip Name", m_AnimClipName);
                if (GUILayout.Button("Rename", GUILayout.Height(35)))
                {
                    m_Clip.name = m_AnimClipName;
                    AssetDatabase.SaveAssets();
                }
                break;
        }

    }
}