小工具01-为Animator Controller添加Animation Clip

最终效果

通常使用Animation创建的Animation Clip与Animator都是分开的,如果用到的Animation Clip比较多的话,文件夹会比较乱,如下图所示。

默认创建Clip效果

通过脚本来把Animator用到的所有Animation Clip都添加为其子资源,如文章开始的动图所示。

新建一个脚本 NestedAnimationCreator.cs,放到Editor文件夹下。

引入命名空间

1
2
3
using UnityEditor;
using UnityEngine;
using UnityEditor.Animations;

创建一个窗口类,用于填写要创建的Clip的名字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class CreateNewAnimationClipWindow : EditorWindow
{
//对话框标题
public string CaptionText { set; get; }
//按钮文本
public string ButtonText { set; get; }
//输入的Clip名字
public string NewName { set; get; }
//按钮点击的委托
public System.Action<string> OnClickButtonDelegate { get; set; }

void OnGUI()
{
NewName = EditorGUILayout.TextField(CaptionText, NewName);
if (GUILayout.Button(ButtonText))
{
if (OnClickButtonDelegate != null)
{
OnClickButtonDelegate.Invoke(NewName.Trim());
}
Close();
GUIUtility.ExitGUI();
}
}
}

创建工具类,用于创建与删除Clip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class NestedAnimationCreator : MonoBehaviour
{
[MenuItem("iTools/Animation/Create Nested Animation Clip")]
public static void Create()
{
//获取在Project面板中选中的AnimatorController
AnimatorController selectedAnimatorController =
Selection.activeObject as AnimatorController;
if (selectedAnimatorController == null)
{
Debug.LogError("Not Select Animator Controller");
return;
}
//打开新建Clip的窗口
CreateNewAnimationClipWindow createNewAnimationClipWindow =
EditorWindow.GetWindow<CreateNewAnimationClipWindow>("Create Nested Animation Clip");
createNewAnimationClipWindow.ButtonText = "Create";
createNewAnimationClipWindow.CaptionText = "New Animation Name";
createNewAnimationClipWindow.NewName = "New Clip";

createNewAnimationClipWindow.OnClickButtonDelegate = (string newName) =>
{
if (string.IsNullOrEmpty(newName))
{
Debug.LogError("Invalid name");
return;
}
//根据输入的名称创建Clip
AnimationClip newClip =
AnimatorController.AllocateAnimatorClip(newName);
//将创建的Clip添加到选中的Controller子资源中
AssetDatabase.AddObjectToAsset(newClip, selectedAnimatorController);
//重新导入资源
AssetDatabase.ImportAsset(
AssetDatabase.GetAssetPath(selectedAnimatorController));
};
}

[MenuItem("iTools/Animation/Delete Animation Clip")]
public static void Delete()
{
Object[] selectedAssets = Selection.objects;
if (selectedAssets.Length < 1)
{
Debug.LogError("Not select asset");
return;
}
foreach (var asset in selectedAssets)
{
if (AssetDatabase.IsSubAsset(asset))
{
string path = AssetDatabase.GetAssetPath(asset);
DestroyImmediate(asset, true);
AssetDatabase.ImportAsset(path);
}
}
}
}
分享到:
Disqus 加载中...

如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理