Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 AUsername64 · Aug 02, 2021 at 01:30 AM · uispritespritesspritesheetgif

Turning GIFs to spritesheets IN UNITY

So GIFs don't work in unity. The work around that gets posted often is convert it into a spritesheet, and while that is very easy to do on the internet, for the sake of my program I need to convert it within unity itself. For example, I want it to be possible to save a GIF to unity in the files and then have unity automatically convert it into a spritesheet and give it back. Can anyone explain how to do this?

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

1 Reply

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

Answer by Bunny83 · Aug 02, 2021 at 10:21 PM

Well GIF is a quite old format and what you probably think about are animated gifs. Though the animation capability are actually an extension and it's even interpreted differently depending on the parser. GIF itself contains several frames which are simply applied on top of each other and a frame could even just be a "diff" frame. GIF essentially represents a virtual canvas and contains "drawing instructions" which are applied one after the other. The animation extension simply allows to add a delay between such operations.


There are some GIF loaders out there, but most of them are not free. I've written a GIF loader from scratch, however it's still essentially "work in progress" for quite some time now since I hadn't much time or interest in polishing the code. However it should work with most GIF files. It essentially just parses all the blocks in the file. You can use the various DrawTo methods of the image or one of the rendering blocks in order to draw it into a Color32 array which can be used to create a texture in Unity.


I just added the "DrawImageTo" method which allows you to reconstruct a certain frame and draw it into a Color32 array at a certain offset. This method will apply all frames in order up to the specifed frame. "DrawPartialFrameTo" only draws a single frame into the color array. Depending on the type of gif this may work. However usually a frame requires the previous frames.


It's up to you how you arrange them into a texture atlas. The size of the image can be read from the "screen" property.


Here's a simple example that draws all the frames into a horizontal strip of images:

     GIFLoader loader = new GIFLoader();
     GifImage image = loader.Load(pathToYourImageFile);
     Texture2D tex = new Texture2D(image.screen.width * image.imageData.Count, image.screen.height);
     var colors = tex.GetPixels32();
     for(int i = 0; i < image.imageData.Count; i++)
     {
         image.DrawImageTo(i, colors, tex.width, tex.height, image.screen.width * i, 0);
     }
     tex.SetPixels32(colors);
     tex.Apply();

If you encounter any issues, feel free to open an issue on the github page. Though please test with other images before you open an issue. If possible provide a sample image that doesn't work / produces errors.

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 AUsername64 · Aug 03, 2021 at 12:04 AM 0
Share

Thanks! This looks like exactly what I needed!

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

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

Override sprite geometry of sprite generated at runtime 1 Answer

"Could not create sprite" exception - copying and manipulating texture from SpriteAtlas 1 Answer

how should I make sprites 0 Answers

Fast Sprites Extraction from Sprite Sheet 0 Answers

add animation frames to existing spritesheet with animations? 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