Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by Gogims · Oct 11, 2015 at 09:40 PM · 2dspritesanimationclipprogrammatically

Create Animation Clip from Sprites[] (Programmatically)

Hello! I have been working on a lite codeless RPG Game Engine which I am building in Unity, using the windows editors that Unity provides. I am have been looking how to change an array of Sprites to an AnimationClip and I don't quite understand it.

I have seen in previous posts (like this one) that you have to use the method "SetCurve". However, in the post mentioned I don't see where does he references that AnimationCurve with the Array of sprites that you want to reproduce the clip.

In short, I would like to reproduce what you do when you select multiple sprites and you drag it to the Hierarchy window (it automatically produces the animation), but in code.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by BauPlay · Oct 19, 2015 at 12:26 PM

Hi! may be this

Save your spritesheet in assets/Resources/sprite

    Sprite[] sprites = Resources.LoadAll<Sprite>("sprite"); // load all sprites in "assets/Resources/sprite" folder

    AnimationClip animClip = new AnimationClip();
    animClip.frameRate = 25;   // FPS

    EditorCurveBinding spriteBinding = new EditorCurveBinding();
    spriteBinding.type = typeof(SpriteRenderer);
    spriteBinding.path = "";
    spriteBinding.propertyName = "m_Sprite"; 

    ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length];
    for(int i = 0; i < (sprites.Length); i++) {
        spriteKeyFrames[i] = new ObjectReferenceKeyframe();
        spriteKeyFrames[i].time = i;
        spriteKeyFrames[i].value = sprites[i];
    }

    AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);

//------------------Now if want, you can save AnimationClip to file .anim

     AssetDatabase.CreateAsset(animClip, "assets/walk.anim");
     AssetDatabase.SaveAssets();
     AssetDatabase.Refresh();


Comment
Add comment · Show 6 · 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 Gogims · Oct 19, 2015 at 06:49 PM 0
Share

Thanks for the code. I have one last question regarding the .anim file, I am trying to create it with loop time checked by default and I thought it was like this:

 animClip.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;

However, when I check the file, it's not checked the loop time property.

avatar image BauPlay · Oct 20, 2015 at 05:38 PM 0
Share

I do not know how to change Wrap$$anonymous$$ode. Also I think about it...

for loop animation, i use this code

     AnimationClipSettings animClipSett = new AnimationClipSettings();
     animClipSett.loopTime = true;
     
     AnimationUtility.SetAnimationClipSettings(animClip, animClipSett);


sorry for my bad english)

avatar image redemptor · Mar 16, 2018 at 02:44 AM 0
Share

why 'm_Sprite' and not 'Sprite'?

avatar image Zynek redemptor · Apr 19, 2018 at 03:46 PM 0
Share

Cause "m_Sprite" works and "Sprite" doesn't ;)

avatar image Zynek · Apr 19, 2018 at 03:50 PM 0
Share

Hi ! Awsome, how did you find out ? I'm trying the same, but i am getting blank sprites in the animation like this Animator Any idea why ? Im doing this on import via OnPostprocessSprites(Texture2D texture, Sprite[] sprites).

screen-shot-2018-04-19-at-174755.png (20.8 kB)
avatar image Zynek Zynek · Apr 20, 2018 at 12:54 PM 0
Share

Solved thanks to this post: https://answers.unity.com/questions/1356394/loading-sprite-from-sprite-sheet.html

Sprites which was being given to me via OnPostprocessSprites(Texture2D texture, Sprite[] sprites) were not valid and actually had negative InstanceID's. So the trick was to load the sprites manually and get the right references.

avatar image
0

Answer by Gogims · Oct 12, 2015 at 07:19 PM

BUMP!! Anyone??

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 Arkasis · Oct 30, 2020 at 03:20 PM

Hello guys, I got some trubbles creating my own animationclip by code.

Eveything is working fine in play mode.

But in the animation inspector, the animationClip is recognize as if it only have a single frame. I have to manually do a random modification on the curves or the frame rate in the inspector in order to make Unity refresh it. It is like if Unity was thinking the animation start and stop time were not good but they are okay. Is there a way to avoid this issue ?

PS : If I restart Unity, everything works but I would like to avoid it.... Thanks :)

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

41 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 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

Instantiating random sprites, with no space between them? 0 Answers

Weird artifacts appear on my 2D sprites. 0 Answers

Fading groups of sprites cheaply? 0 Answers

some sprites are turning transparent while using a normal map and others don't 0 Answers

Sprites & Animation 1 Answer


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