Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 4illeen · Feb 21, 2012 at 12:45 AM · 2dspritetexture2d

Texture2D Sprite

I'm about to do some 2D animations. Currently I use a plane with a texture on it, and change the texture via .Renderer.material.mainTexture = otherTexture;.

I'm curious if it's possible to store all the sprites I need in one file and do something like this: alt text

The big texture contains all the animation states/sprites but only part of it is seen on the Plane surface. Then we 'move' the texture inside the Plane by x pixels so other content is visible. I have a lot of animations and loading every one as a separate Texture2D doesn't seem like the best solution. Thanks in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by James Tima · Feb 21, 2012 at 09:47 AM

use animation sheet like this

95link text95

and script is

 //vars for the whole sheet
 
 var colCount    : int =  4;
 
 var rowCount    : int =  4;
 
 //vars for animation
 
 var rowNumber   : int =  0; //Zero Indexed
 
 var colNumber   : int =  0; //Zero Indexed
 
 var totalCells  : int =  4;
 
 var fps  : int = 10;
 
 var offset  : Vector2;  //Maybe this should be a private var
 
 //Update
 
 function Update () 
 
 {
 
 SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  
 
     
 }
 
 
 function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int)
 {
 
     // Calculate index
     var index : int = Time.time * fps;
     // Repeat when exhausting all cells
     index = index % totalCells;
 
     // Size of every cell
     var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
 
     // split into horizontal and vertical index
     var uIndex = index % colCount;
     var vIndex = index / colCount;
 
     // build offset
     // v coordinate is the bottom of the image in opengl so we need to invert.
     offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
 
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale  ("_MainTex", size);
 }
 
 
 
 
     
 
 
   

Comment
Add comment · Show 2 · 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 POLYGAMe · Feb 21, 2012 at 10:25 AM 0
Share

You could store each sprite frame as a separate texture in an array and count through with a for loop too. That way you can swap out frames if you need to make changes etc.

avatar image 4illeen · Feb 21, 2012 at 12:00 PM 0
Share

"You could store each sprite frame as a separate texture in an array and count through with a for loop too."

this is what I'm doing right now, but I feel like it's bad for performance due to huge amount of textures I have to load

avatar image
1

Answer by robertbu · Mar 25, 2013 at 04:22 PM

If you are using the same material multiple places, then going to a "texture atlas" solution will likely reduce draw calls and potentially improve performance. There are a number of different scripts around that implement this functionality both on UnityAnswers and in the Wiki. They animate the UV coordinates of the material. Here is one link:

http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture_-_Extended

Note third-party tools like EZGUI and probably NGUI automate the process of creating animated textures and building the atlas. That is, the frames exist as individual textures in the editor, but the tools build a single atlas/texture which is used at runtime.

To to be clear, in order to get a significant win out of this, you need to have multiple animations using the same animation sheet (and therefore they both would have exactly the same material).

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

8 People are following this question.

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

Related Questions

Drawing onto the Texture2D of a sprite 1 Answer

Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer

Strange edge on texture after hiding pixels 3 Answers

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer

My sprite from sprite.create looks pixelated. 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