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 romofan23 · Jul 04, 2013 at 01:19 AM · spritesmaterialstextures

How to quickly change the texture on a material?

For my 2D game, I am making my player sprite. In my script, I try to change the texture on the material every 1/10 of a second, but it keeps glitching. I need it to loop. Here is the code:

 var walk1 : Texture2D;
 var walk2 : Texture2D;
 var walk3 : Texture2D;
 
 function Update () {
     if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
         renderer.material.mainTexture = walk1;
         wait(0.1);
         renderer.material.mainTexture = walk2;
         wait(0.1);
         renderer.material.mainTexture = walk3;
         wait(0.1);
         renderer.material.mainTexture = walk2;
         wait(0.1);
     }
 }
 
 function wait(amount : float) {
     yield WaitForSeconds(amount);
 }
Comment
Add comment · Show 1
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 romofan23 · Jul 04, 2013 at 02:50 AM 0
Share

Okay, I tried the code. One problem, it won't loop when I hold the Input $$anonymous$$ey. How would I fix that?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jul 04, 2013 at 01:35 AM

You need to do a bit of research on coroutines. The call to wait() returns immediately. Any lines of code after the yield (which you don't have any here) would be execute after the specified amount of time. But still the the initial call to wait() returns the first time a yield is hit. So this code will not wait.

Here is a post doing something similar with an array of textures. The textures are initialized in the Inspector:

http://answers.unity3d.com/questions/392517/introduction-movie.html

In addition there are several scripts for animation in the Unity Wiki. Here is one:

http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture

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 robertbu · Jul 04, 2013 at 02:12 AM 1
Share

You could get your code working like this:

 #pragma strict
 
 var walk1 : Texture2D;
 var walk2 : Texture2D;
 var walk3 : Texture2D;
  
 function Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
          Display();
      }
 }
  
 function Display() {
        renderer.material.mainTexture = walk1;
        yield WaitForSeconds(0.1);
        renderer.material.mainTexture = walk2;
        yield WaitForSeconds(0.1);
        renderer.material.mainTexture = walk3;
        yield WaitForSeconds(0.1);
        renderer.material.mainTexture = walk2;
        yield WaitForSeconds(0.1);
 }
avatar image romofan23 · Jul 04, 2013 at 02:50 AM 0
Share

Okay, I tried the code. One problem, it won't loop when I hold the Input $$anonymous$$ey. How would I fix that?

avatar image robertbu · Jul 04, 2013 at 04:10 AM 0
Share

There are a number of ways to implement what I think you are going for. It makes it far easier if you put your images in an array. Here is another version:

 #pragma strict
 
 var walks : Texture2D[];
 
 private var itex = 0;
 private var walking = false;
 
 function Start() {
     renderer.material.mainTexture = walks[0];
     Display();
 }
  
 function Update () {
     walking = false;
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
          walking = true;
      }
 }
  
 function Display() {
     while (true) {
         if (walking) {
             itex = (itex + 1) % walks.Length;
                renderer.material.mainTexture = walks[itex];
            }
            yield WaitForSeconds(0.1);
     }
 }

After you have this attached to the plane, you need to select the plane in the inspector. Click on the triangle next to 'walks', set the size of the array for the number of images, and then drag and drop those images into the slots.

avatar image robertbu · Jul 04, 2013 at 04:12 AM 0
Share

Here is a slightly different take that uses InvokeRepeating().

 #pragma strict
 
 var walks : Texture2D[];
 
 private var itex = 0;
 private var walking = false;
 
 function Start() {
     renderer.material.mainTexture = walks[0];
     InvokeRepeating("Display", 0.0, 0.1);
 }
  
 function Update () {
     walking = false;
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
          walking = true;
      }
 }
  
 function Display() {
         if (walking) {
             itex = (itex + 1) % walks.Length;
                renderer.material.mainTexture = walks[itex];
            }
 }

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

15 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

Related Questions

Change the alpha of part of a material or texture or applying part of a material at runtime 0 Answers

Ideal resolutions for 2D platformer Art Assets (to make multi-res support easier)? 2 Answers

Best method for implementing alternative view modes? (like Cities: Skylines "Info View") 0 Answers

What is the best way of creating a modular texture? 0 Answers

Unity Trees Material/Texture changed 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