• 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 drift501 · May 10, 2012 at 11:06 AM · texturemesh

Switching Between Textures

Hello. I have got a plane (flat mesh not aircraft) and 10 textures that I want to swap quickly and after all ten start again so I can simulate a waterfall. How can I get all of the textures to cycle one texture every 0.1 of a second and then loop.

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 zz74b · May 10, 2012 at 11:42 AM

in c#

 private Texture[] _textureArray = Texture[] { texture1, texture2, texture3, ... etc };
     
 void Awake()
 {
    StartCoroutine(Cycler());
 }
 
 IEnumerator Cycler()
 {
     int currentTex = 0;
     while(true)
     {
         yield return new WaitForSeconds(0.1f);
         currentTex++;
         var tex = _textureArray[currentTex % _textureArray.Length];
         .. Assign texture here.
     }
 }

Explaination:

Start a co-rountine that loops for ever, waiting 0.1 seconds before it runs each time. Then using modulo maths, selects the next texture from the array of textures. It doesn't matter how many textures you use, it'll always loop correctly.

Assign 'tex' to your plane in the normal way.

[Disclaimer: not tested in unity - may not compile :P]

Comment
Add comment · Show 7 · 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 zz74b · May 10, 2012 at 11:57 AM 0
Share

warning: if you run this code for ages, you may get an integer overflow. I'll leave it up to you to work out how to cope with that situation.

avatar image Piflik · May 10, 2012 at 12:06 PM 0
Share

Just add

 if(currentTex == _textureArray.Length)
     currentTex = 0;

after curentTex++;.

You also don't need modulo maths then.

avatar image syclamoth · May 10, 2012 at 12:21 PM 0
Share

RE: the integer overflow problem- with normal signed 32-bit integers, you will have to run the program for about SEVENTY YEARS at one increment every 1/10 of a second before that becomes an actual issue. Just saying.

avatar image zz74b · May 10, 2012 at 12:35 PM 1
Share

Here I am trying to get newbs to think about the code they are writing and you guys come wading in and giving them all the answers.. :) - What do you think this is? A question and answer site??!

The reason I suggested modulo maths, is in the hope that the OP would go and look up what it is all about and become a better programmer..

avatar image drift501 · May 11, 2012 at 07:17 AM 0
Share

Thanks for this. I dont know if this is just a problem with my computer but I found the sprite sheet script on the wiki and that had better performance than this script.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to "paint" the terrain in a script? 1 Answer

Any script to 'fix' normals/winding order? 3 Answers

Animated Texture Offset 1 Answer

Mesh and material disappear after CreatePrefab() in C# 1 Answer

how to show a web browser on unity 3D game 4 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