- Home /
Create a Sprite Animation Clip in Code
I am trying to construct an Animation clip in C#
AnimationClip animClip = new AnimationClip();
Keyframe[] ks;
ks = new Keyframe[4];
ks[0] = new Keyframe(0f, 0f);
ks[1] = new Keyframe(1f, 1f);
ks[2] = new Keyframe(2f, 2f);
ks[3] = new Keyframe(3f, 3f);
AnimationCurve _curve = new AnimationCurve(ks);
animClip.SetCurve("TestAnimation2", typeof(SpriteRenderer), "Sprite", _curve);
animClip.wrapMode = WrapMode.Loop;
game_object.AddComponent ("Animation");
game_object.animation.AddClip (animClip, "AnimationCurve7");
game_object.animation.Play("AnimationCurve7");
The above works? however :
How do I add a different sprite to each keyframe?
How do I add a different sprite to each keyframe?
What do you mean by adding a different sprite to keyframe do you mean to reference them
I'm attempting to construct a sprite animation create purely by code, I need 3 sprite frames each with a different sprite, I can't work out how to attach the image to each keyframe (in the animation editor you just drop the image onto the keyframe, how do i do this in code ?)
I have a different way of doing this, here's a basic script
//Get the sprites, set the amount of sprites in the editor then drag and drop
public Sprite[] spr = new Sprite[]{};
//Set the amount again, remember arrays start at 0, so if there is 10 sprites you'd set this to 9.
public int sprLim;
//Sets the speed that the sprites change
public float sprSpeed;
//Get the sprite renderer
private SpriteRenderer sprRend;
void Awake()
{
sprRend = GetComponent<SpriteRenderer>();
StartCoroutine("SprAnim");
}
IEnumerator SprAnim()
{
while(true) //$$anonymous$$eeps it running
{
for (i = 0; i <= sprLim; i++) //Set up a for loop
{
sprRend.sprite = spr[i]; //Sets the sprite
yield return new WaitForSeconds(sprSpeed); //Waits set amount of time
}
yield return new WaitForSeconds (0.0f); //if the sprite jumps, change 0.0f to sprSpeed
}
}
With this you might be able to work something out
I really want to use the built in functions, it just seems very difficult to code an animation and I have too many to manually construct them, but may have too
I want to do that shit too, but I have no way... Its not possible to change an sprite with setcurve, looks like a curve can only have float values.
Answer by Xelnath · Feb 28, 2015 at 01:27 PM
take a look at object reference keys
ArrayList curves = (ArrayList)paths[path];
for (int i = 0; i < curves.Count; i++) {
EditorCurveBinding binding = (EditorCurveBinding)curves[i];
AnimationCurve curve = AnimationUtility.GetEditorCurve(animationClip, binding);
ObjectReferenceKeyframe[] objectReferenceCurve = null;
if (curve != null) {
AnimationUtility.SetEditorCurve(animationClip, binding, null);
} else {
objectReferenceCurve = AnimationUtility.GetObjectReferenceCurve(animationClip, binding);
AnimationUtility.SetObjectReferenceCurve(animationClip, binding, null);
}
if (path == oldPath) {
binding.path = newPath;
}
if (curve != null) {
AnimationUtility.SetEditorCurve(animationClip, binding, curve);
} else {
AnimationUtility.SetObjectReferenceCurve(animationClip, binding, objectReferenceCurve);
}
}
I see the work Editor in there a lot. is this something that can be used in a build or just the editor?
Answer by andoitz · Jan 14, 2015 at 07:22 PM
Ey man, I do that. I think this is good, this is my code:
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
public class CharAnimation : MonoBehaviour {
/*
Character sprites positions:
0 => Walking down 1
1 => Standing down
2 => Walking down 2
3 => Walking left 1
4 => Standing left
5 => Walking left 2
6 => Walking right 1
7 => Standing right
8 => Walking right 2
9 => Walking up 1
10 => Standing up
11 => Walking up 2
*/
private string currentAnimation = "StandDown";
private int frame = 0;
private int animationFrames = 20; // PAR NUMBER
private Sprite[] sprites = new Sprite[12];
private SpriteRenderer rend;
Animator animator = new Animator();
public void Awake(){
rend = (SpriteRenderer) this.GetComponent<SpriteRenderer>();
}
public void Start () {
}
public void Update(){
if(frame == animationFrames) frame = 0;
frame++;
callAnimation();
}
private void callAnimation(){
if(currentAnimation == "WalkUp") WalkUp();
else if(currentAnimation == "WalkDown") WalkDown();
else if(currentAnimation == "WalkLeft") WalkLeft();
else if(currentAnimation == "WalkRight") WalkRight();
else if(currentAnimation == "StandUp") StandUp();
else if(currentAnimation == "StandDown") StandDown();
else if(currentAnimation == "StandLeft") StandLeft();
else if(currentAnimation == "StandRight") StandRight();
}
private void changeOnFrame(int f1, int f2){
if(frame == 1) rend.sprite = sprites[f1];
else if(frame == animationFrames/2) rend.sprite = sprites[f2];
else if(frame == animationFrames) rend.sprite = sprites[f1];
}
private void WalkUp(){
changeOnFrame(9,11);
}
private void WalkDown(){
changeOnFrame(0,2);
}
private void WalkLeft(){
changeOnFrame(3,5);
}
private void WalkRight(){
changeOnFrame(6,8);
}
private void StandUp(){
if(frame == 1) rend.sprite = sprites[10];
}
private void StandDown(){
if(frame == 1) rend.sprite = sprites[1];
}
private void StandLeft(){
if(frame == 1) rend.sprite = sprites[4];
}
private void StandRight(){
if(frame == 1) rend.sprite = sprites[7];
}
public void setSprites(Sprite[] sprites){
this.sprites = sprites;
}
public void changeAnimation(string animation){
frame = 0;
currentAnimation = animation;
}
}
Answer by Xelnath · Feb 28, 2015 at 12:36 PM
It might help if you look at:
EditorCurveBinding[] _Curves2 = AnimationUtility.GetObjectReferenceCurveBindings (anim);
Your answer
Follow this Question
Related Questions
Animation Cycle Dilemma 0 Answers
Abrupt change of position between animation transition 2 Answers
Animated Overlay Armor Sprite 1 Answer
How do I animate a 2D sprite? 1 Answer
Animation stoping!! 1 Answer