Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Animations;
#endif
public class CustomAnimatorWindow : EditorWindow
{
string savePath = "Assets/Animations/";
RuntimeAnimatorController runtimeAnimCtrl = null;
//WIndow設定
[MenuItem("Window/CustomAnimatorWindow")]
static void Open()
{
EditorWindow.GetWindow<CustomAnimatorWindow>("CustomAnimator");
}
void OnGUI()
{
printCustomLabel("Stateと同じ名前のAnimationClipを生成", Color.white, Color.black, 18);
savePath = EditorGUILayout.TextField("保存するディレクトリ", savePath);
runtimeAnimCtrl = (RuntimeAnimatorController)EditorGUILayout.ObjectField("Stateを取得する対象のAnimationController", runtimeAnimCtrl, typeof(RuntimeAnimatorController), false);
if (GUILayout.Button("Clip生成"))
{
AnimatorController animCtrl = runtimeAnimCtrl as AnimatorController;
AnimatorControllerLayer layer = animCtrl.layers[0]; //todo 複数レイヤー
AnimatorStateMachine stateMachine = layer.stateMachine;
//Stateを取得し、State名がついたClipファイルを生成+Stateにセットする
foreach (var state in stateMachine.states)
{
AnimatorState animState = state.state;
AnimationClip animclip = new AnimationClip();
state.state.motion = (Motion)animclip; //空のClipをセット
AssetDatabase.CreateAsset(animclip, AssetDatabase.GenerateUniqueAssetPath(savePath + animState.name + ".anim"));
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
//Label装飾用のラッパー
void printCustomLabel(string text, Color textColor, Color backColor, int fontSize, FontStyle fontStyle = FontStyle.Bold)
{
Color beforeBackColor = GUI.backgroundColor;
GUIStyle guiStyle = new GUIStyle();
GUIStyleState styleState = new GUIStyleState();
styleState.textColor = textColor;
styleState.background = Texture2D.whiteTexture;
GUI.backgroundColor = backColor;
guiStyle.normal = styleState;
guiStyle.fontSize = fontSize;
GUILayout.Label(text, guiStyle); //labelFieldだとうまくいかない?
GUI.backgroundColor = beforeBackColor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment