Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JHibbins · Apr 25, 2014 at 01:46 PM · animationsprite

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deltamish · Apr 25, 2014 at 01:50 PM 0
Share

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

avatar image JHibbins · Apr 25, 2014 at 01:54 PM 0
Share

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 ?)

avatar image BlackHoleStorm · Apr 25, 2014 at 02:00 PM 0
Share

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

avatar image JHibbins · Apr 25, 2014 at 02:18 PM 0
Share

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

avatar image andoitz · Jan 14, 2015 at 04:54 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
             }
             
         }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Da_Elf · May 30 at 01:16 AM 0
Share

I see the work Editor in there a lot. is this something that can be used in a build or just the editor?

avatar image
0

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;
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Xelnath · Feb 28, 2015 at 12:36 PM

It might help if you look at:

EditorCurveBinding[] _Curves2 = AnimationUtility.GetObjectReferenceCurveBindings (anim);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation Cycle Dilemma 0 Answers

Sprite Animation - Problem with image progression 1 Answer

Changing number of frames in uvAnimationTileX 0 Answers

Can't animate Sprite correctly 0 Answers

Why do iOS scales my sprites while they are working fine on Unity Editor and Android? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges