Best way to dynamically change textures

Hi All,

I’ve got a plane and assigned a “Transparent/Cutout/VertexLit” material along with an image.

How do I change the image in the material dynamically via scripting?

Thanks a lot!

-Mel :wink:

First off, welcome to the forums!

Second, can you offer more detail on how you want to “change the image”? Do you have another texture in your project and you just want to swap what’s shown? Do you want to use GetPixel/SetPixel operations to “draw” into the texture to change it? Do you want to load an external image file (JPG/PNG) on the fly and use that as a texture? Other?

Offering more detail as to what you’re after will help everyone provide appropriate guidance so let us know!

Hi HiggyB!

I want to make a 2D animation controller using a plane. So it would basically flip through a series of png files to animate the sprite.

The material would stay the same, but it’s just the image file I want to load dynamically so the 2D plane will animate.

Hope that makes more sense!

-Mel :wink:

Heay Mel,

Have a look on the wiki for the animated tiled texture example? It might be useful to ransack for code snippets…

And a welcome from me too!

AaronC

( http://unifycommunity.com )

Aaron is referring to this and it’s a good thing to check out. Although instead of flipping through a series of textures you have one texture (16 x 128 for example, really just a 1 x 8 series of textures instead of 8 separate textures) and you adjust an offset value to achieve the “animation” goal.

The other way to go about this would be to initialize an array of textures that you’ll use. The you can write a simple function that iterates through the array, each iteration it sets the material’s mainTexture property and it then uses yield and WaitForSeconds to pause and give the desired “frame rate”.

That make sense? If not then let us know and we’ll keep at it!

Hi Higgy!

Yeh, I was looking in to the initializing textures way of doing things! I’m going to attempt scripting this on my own, and I’ll reply if I run into any problems!

Thanks for the help guys!

-Mel :wink:

i have 2 questions to the tiles texture in the wiki:

  1. is it possible to make a 256x256 texture with this fire animations?

  2. when i apply the texture in unity to a plane, alle fire stations will be seen. Must i uv map the texture in maya?

The script will animate it at playtime. See the inspector for setting the width/height. Experiment with those values if neccess