Skip to content

Instantly share code, notes, and snippets.

@partlyhuman
Created October 26, 2017 20:37
Show Gist options
  • Save partlyhuman/1e2ad15ca858cc708cc9deb6bdd4b9a9 to your computer and use it in GitHub Desktop.
Save partlyhuman/1e2ad15ca858cc708cc9deb6bdd4b9a9 to your computer and use it in GitHub Desktop.
Work with Animation Clips embedded in an AnimatorController asset
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
namespace com.hololabs
{
public static class EmbeddedAnimationClipUtility
{
[MenuItem("Assets/Add Embedded Animation Clip", false)]
public static void AddEmbeddedClip()
{
var container = Selection.activeObject as AnimatorController;
if (!container) return;
TextInputDialog.Prompt("New clip",
"Name for new embedded clip:",
name =>
{
AssetDatabase.AddObjectToAsset(new AnimationClip {name = name}, container);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("Done");
});
}
[MenuItem("Assets/Add Embedded Animation Clip", true)]
public static bool AddEmbeddedClip_Validate()
{
Object obj = Selection.activeObject;
return (obj && obj is AnimatorController && AssetDatabase.Contains(obj));
}
[MenuItem("Assets/Remove Embedded Animation Clip", false)]
public static void RemoveEmbeddedClip()
{
foreach (Object clip in Selection.objects)
{
Object.DestroyImmediate(clip, true);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("Done");
}
[MenuItem("Assets/Remove Embedded Animation Clip", true)]
public static bool RemoveEmbeddedClip_Validate()
{
return Selection.objects.All(obj => obj && obj is AnimationClip && AssetDatabase.Contains(obj) && AssetDatabase.IsSubAsset(obj));
}
}
}
using System;
using UnityEditor;
using UnityEngine;
namespace com.hololabs
{
public class TextInputDialog : EditorWindow
{
const string INPUT_NAME = "TextInputDialog_TextField";
public string promptText = "Enter your input:";
public Action<string> callback;
void OnGUI()
{
EditorGUILayout.BeginVertical(GUIStyle.none, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
EditorGUILayout.LabelField(promptText);
GUI.SetNextControlName(INPUT_NAME);
string inputText = EditorGUILayout.DelayedTextField("");
EditorGUILayout.EndVertical();
if (string.IsNullOrEmpty(inputText))
{
EditorGUI.FocusTextInControl(INPUT_NAME);
}
else
{
callback(inputText);
Close();
}
}
void OnLostFocus()
{
Close();
}
public static void Prompt(string title, string promptText, Action<string> callback)
{
var window = CreateInstance<TextInputDialog>();
window.minSize = new Vector2(300, 50);
window.maxSize = window.minSize;
window.titleContent = new GUIContent(title);
window.callback = callback;
window.promptText = promptText;
window.ShowUtility();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment