Created
March 18, 2023 00:28
-
-
Save Evolis3d/5ca204818f02c77a1898c10769e08cb3 to your computer and use it in GitHub Desktop.
Generate an AnimClip asset from a selection of Sprites at the ContextMenu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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