Skip to content

Instantly share code, notes, and snippets.

@Evolis3d
Created March 18, 2023 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Evolis3d/5ca204818f02c77a1898c10769e08cb3 to your computer and use it in GitHub Desktop.
Save Evolis3d/5ca204818f02c77a1898c10769e08cb3 to your computer and use it in GitHub Desktop.
Generate an AnimClip asset from a selection of Sprites at the ContextMenu
using UnityEditor;
using UnityEngine;
namespace Evolis3D.Utils
{
[CanEditMultipleObjects]
public class misc_AnimTools : MonoBehaviour
{
[MenuItem("Assets/!Evolis3D/Compound AnimClip with these...")]
static void CompoundAnimClip()
{
var frames = Selection.GetFiltered<Sprite>(SelectionMode.ExcludePrefab);
if (frames.Equals(null) || frames.Length < 1) return;
Debug.Log($"<color=yellow>[CompoundClip]</color> Selected: {frames.Length.ToString()} sprites. <color=cyan>✓</color>");
var clip = new AnimationClip();
clip.frameRate = 1;
clip.wrapMode = WrapMode.Loop; //by default loops
var animClipSett = new AnimationClipSettings();
animClipSett.loopTime = true;
AnimationUtility.SetAnimationClipSettings(clip, animClipSett);
var keys = new Keyframe[frames.Length];
var spriteBinding = new EditorCurveBinding();
spriteBinding.type = typeof(SpriteRenderer);
spriteBinding.path = "";
spriteBinding.propertyName = "m_Sprite";
//spriteBinding.propertyName = "Sprite";
var spriteKeyFrames = new ObjectReferenceKeyframe[frames.Length];
for(int i = 0; i < (frames.Length); i++) {
spriteKeyFrames[i] = new ObjectReferenceKeyframe();
spriteKeyFrames[i].time = i/clip.frameRate;
spriteKeyFrames[i].value = frames[i];
}
AnimationUtility.SetObjectReferenceCurve(clip, spriteBinding, spriteKeyFrames);
var filepath = EditorUtility.SaveFilePanelInProject("Save AnimClip as...", "clip_xxx", "anim", "anim");
if (filepath.Equals(null)) return;
AssetDatabase.CreateAsset(clip,filepath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log($"<color=yellow>[CompoundClip]</color> AnimClip asset generated. <color=cyan>✓</color>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment