I’m trying to build animations in the editor directly in the code using some information passed in via the user.
I am dealing with spritesheets, and looking for a way to automatically create animations from the spritesheets. I am leveraging the answer found here
So far, here is the code I have created. It generates and saves an animation, but the animation appears to be blank. What am I doing wrong?
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
//Copy and paste atlas settings to another atlas editor
public class AutoAnimationCreator : EditorWindow
{
public Texture2D spriteSheet; //Sprite Atlas to copy from settings
public int frameHeight;
public int frameWidth;
public Texture2D pasteTo; //Sprite atlas where to paste settings
private Sprite[] _sprites; //Collection of sprites from source texture for faster referencing
[MenuItem ("Window/Sprite Animator")]
static void Init()
{
// Window Set-Up
AutoAnimationCreator window = EditorWindow.GetWindow(typeof(AutoAnimationCreator), false, "AnimationGenerator", true) as AutoAnimationCreator;
window.minSize = new Vector2(260, 170); window.maxSize = new Vector2(260, 170);
window.Show();
}
//Show UI
void OnGUI ()
{
spriteSheet = (Texture2D)EditorGUILayout.ObjectField ("SpriteSheet", spriteSheet, typeof(Texture2D), true);
EditorGUILayout.Space ();
if(GUILayout.Button ("Generate Animation"))
{
if(spriteSheet != null)
{
cutSprites ();
makeAnimation();
}
}
else {
Debug.LogWarning("Forgot to set the textures?");
}
Repaint ();
}
private void cutSprites()
{
if(!IsAtlas (spriteSheet))
{
Debug.LogWarning ("Unable to proceed, the source texture is not a sprite atlas.");
return;
}
//Proceed to read all sprites from CopyFrom texture and reassign to a TextureImporter for the end result
UnityEngine.Object[] _objects = AssetDatabase.LoadAllAssetRepresentationsAtPath (AssetDatabase.GetAssetPath (spriteSheet));
if(_objects != null && _objects.Length > 0)
_sprites = new Sprite[_objects.Length];
for (int i = 0; i < _objects.Length; i++)
{
_sprites <em>= _objects *as Sprite;*</em>
}
}
private void makeAnimation()
{
AnimationClip clip = new AnimationClip();
//http://forum.unity3d.com/threads/lack-of-scripting-functionality-for-creating-2d-animation-clips-by-code.212615/*_
AnimationClip animClip = new AnimationClip();
_// First you need to create e Editor Curve Binding*
EditorCurveBinding curveBinding = new EditorCurveBinding();
// I want to change the sprites of the sprite renderer, so I put the typeof(SpriteRenderer) as the binding type.
curveBinding.type = typeof(SpriteRenderer);
// Regular path to the gameobject that will be changed (empty string means root)
curveBinding.path = “”;
// This is the property name to change the sprite of a sprite renderer
curveBinding.propertyName = “m_Sprite”;
// An array to hold the object keyframes
ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[10];
for (int i = 0; i < 10; i++)
{
keyFrames = new ObjectReferenceKeyframe();
// set the time
keyFrames*.time = i;*
// set reference for the sprite you want
keyFrames.value = sprites ;
Debug.LogWarning (sprites );
}
AnimationUtility.SetObjectReferenceCurve(animClip, curveBinding, keyFrames);
AssetDatabase.CreateAsset(clip,“Assets/Animations/Animations/test2.anim”);
}
//Check that the texture is an actual atlas and not a normal texture
private bool IsAtlas (Texture2D tex)
{
string _path = AssetDatabase.GetAssetPath(tex);
TextureImporter _importer = AssetImporter.GetAtPath(_path) as TextureImporter;
return _importer.textureType == TextureImporterType.Sprite && _importer.spriteImportMode == SpriteImportMode.Multiple;
}
}