• 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 Unity Gamer · Jun 25, 2013 at 10:50 AM · spriteuv

Shifting UV of a Plane for Sprite Animation

How would you shift the UV of a plane for sprite animation using the offset prescribed by the code here. More Specifically, this code

 using UnityEngine;
 using System.Collections;
  
 class AnimateTiledTexture : MonoBehaviour
 {
 
 public int columns = 2;
     public int rows = 2;
     public float framesPerSecond = 10f;
  
     //the current frame to display
     private int index = 0;
  
     void Start()
     {
         StartCoroutine(updateTiling());
  
         //set the tile size of the texture (in UV units), based on the rows and columns
         Vector2 size = new Vector2(1f / columns, 1f / rows);
         renderer.sharedMaterial.SetTextureScale("_MainTex", size);
     }
  
     private IEnumerator updateTiling()
     {
         while (true)
         {
             //move to the next index
             index++;
             if (index >= rows * columns)
                 index = 0;
  
             //split into x and y indexes
             Vector2 offset = new Vector2((float)index / columns - (index / columns), //x index
                                           (index / columns) / (float)rows);          //y index
  
             renderer.sharedMaterial.SetTextureOffset("_MainTex", offset);
  
             yield return new WaitForSeconds(1f / framesPerSecond);
         }
  
     }
 }

I understand that I use Mesh.uv to manipulate the co - ordinates. I did so with a pre determined offset for a static sprite, but am having problems with animated sprites.

Any ideas??

Thanks for the help in adbvance.

Cheers!!

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
0

Answer by Santa · Jun 25, 2013 at 11:40 AM

There is an error in calculating offset. To get right column you need a remainder. And another thing for row.

 offset.x = ((float)(index % columns)) / (float)columns;
 offset.y = ((float)(index / columns)) / (float)rows;


And by the way if you do renderer.sharedMaterial.SetTextureOffset() you will change all the GameObjects with this sprite. Do renderer.material.SetTextureOffset() instead.

Comment
Add comment · Show 4 · 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 Unity Gamer · Jun 25, 2013 at 11:44 AM 0
Share

I have modified the code above for it to work properly.

And I know how to animate using "Texture Offsets". I want to do so with the help of the uv coords given by $$anonymous$$esh.uv. I want to reduce the number of draw calls, I cant use renderer.shared$$anonymous$$aterial.SetTextureOffset, because it changes all the sprites and I am using a texture atlas

avatar image Santa · Jun 25, 2013 at 12:11 PM 0
Share

As I understand you don't want to touch material.textureOffset at all? And change only mesh.uv? So you need to recalculate offset in the same way for each of vertex. It depends on how there are positioned in the space. But it's the same offset on the texture.

avatar image Unity Gamer · Jun 25, 2013 at 12:15 PM 0
Share

Tried using the offsets I calculated, its not working.

Here's the code.

         while (true)
         {                    
             ApplyOffset(spriteAnimate);            
             
             //move to the next index
             if(Input.Get$$anonymous$$ey("right"))
             {
                 //Debug.Log(index);
                 index++; 
                 count++;
                 
                 $$anonymous$$eshFilter meshFilter = spriteAnimate.GetComponent("$$anonymous$$eshFilter") as $$anonymous$$eshFilter;
                 $$anonymous$$esh quad = meshFilter.mesh;
                 Vector2[] uvs = new Vector2[quad.uv.Length];                                    
                 
                 for(int i=0; i<uvs.Length; i++)
                 {
                     uvs[i].x = quad.uv[i].x + offset.x;
                     uvs[i].y = quad.uv[i].y + offset.y;;
                 }
                 
                 quad.uv = uvs;
                 
                 if (count > maxCount)
                 {
                     end = index;
                     index = start;
                     count = 0;
                 }
             }
         
             yield return new WaitForSeconds(1f / framesPerSecond);            
         }
avatar image Santa · Jun 25, 2013 at 12:47 PM 0
Share

I've tried this in editor script: (WARNING! Changes shared$$anonymous$$esh. Use only on test objects.) Object changes so it works. Check your calculations.

     [$$anonymous$$enuItem("Custom/Sprites/_Archive/TEST - UV animate")]
     static public void TEST_UV_Animate_$$anonymous$$enuItem()
     {
         if (!Selection.activeObject) {EditorUtility.DisplayDialog("Select object", "You must select objects first.", "Ok"); return;}
         GameObject obj = Selection.activeObject as GameObject;
         if (!obj) {EditorUtility.DisplayDialog("Select object", "You must select __GameObject__.", "Ok"); return;}
 
         $$anonymous$$eshFilter meshFilter = obj.GetComponent("$$anonymous$$eshFilter") as $$anonymous$$eshFilter;
         if (!meshFilter) {EditorUtility.DisplayDialog("", "No $$anonymous$$eshFilter.", "Ok"); return;}
 
         //leak error in editor //$$anonymous$$esh quad = meshFilter.mesh;
         $$anonymous$$esh quad = meshFilter.shared$$anonymous$$esh;
         if (!quad) {EditorUtility.DisplayDialog("", "No $$anonymous$$esh.", "Ok"); return;}
         if (quad.uv == null) {EditorUtility.DisplayDialog("", "No quad.uv.", "Ok"); return;}
         if (quad.uv.Length <= 0) {EditorUtility.DisplayDialog("", "quad.uv.lengh <= 0", "Ok"); return;}
 
         Vector2[] uvs = new Vector2[quad.uv.Length]; 
 
         for(int i=0; i<uvs.Length; i++)
         {
             //uvs[i].x = quad.uv[i].x + 0.1f;
             //uvs[i].y = quad.uv[i].y + 0.1f;
             uvs[i].x = Random.Range(0.1f, 0.9f);
             uvs[i].y = Random.Range(0.1f, 0.9f);
 
             Debug.Log("i "+i+". Was = "+quad.uv[i]+". New = "+uvs[i]);
         }
  
         quad.uv = uvs;
         Debug.Log("set");
 
     }
 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Sprite shader relative UV's 0 Answers

Assigning UV Map to model at runtime 0 Answers

Accessing a second texture's offset in a modified sprite shader 0 Answers

Efficient way to keep a fixed rotation for sprite? 0 Answers

Shifting the UV's of a plane 2 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