- Home /
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);
}
Okay, I tried the code. One problem, it won't loop when I hold the Input $$anonymous$$ey. How would I fix that?
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
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);
}
Okay, I tried the code. One problem, it won't loop when I hold the Input $$anonymous$$ey. How would I fix that?
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.
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
Follow this Question
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