- Home /
randomly changing texture of material
hi folks!
i want to set up a simple card game, searching for the best method of texturing the different card fronts. i was wondering if it would be the best to put all the different card front pictures (which should be placed randomly later) in one big texture an then show only one part of the texture in the material (like masking). is this the best way and if so - how could i do that!? :-)
thnx!
edited: just for the record: if i want to change the Offset of a texture in a material, how could i do that?
or isn't it also possible to get something to work with
GetComponentInChildren
Answer by spinaljack · Apr 22, 2010 at 02:28 PM
Grouping textures is generally good practice to reduce memory requirements but mostly for things like particle or flare effects. A way to randomise textures for cards would be to set up an array containing all your card textures, you can do this by declaring an array called var cardMats: material[]; then you can assign any number of them manually in the inspector.
Then when you want to apply a material you'd generate a random number to pick one from the array, something like this:
mat = Random.Range(0, cardMats.length)
static function Range (min : int, max : int) : int Description Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).
If max equals min, min will be returned. The returned value will never be max unless min equals max. So you wont have to worry about array out of bounds this this method.
You assign the material to the renderer with this:
renderer.material = cardMats[mat];
thank you, i will try to try that... just for the record: if i want to change the Offset of a texture in a material, how could i do that?
Answer by 3ddf · Apr 22, 2010 at 02:28 PM
Hi headkit,
This post on the unity forums explains the texture atlasing stuff fairly well, it should point you in the right direction I think.
http://forum.unity3d.com/viewtopic.php?t=7207&postdays=0&postorder=asc&start=0
phuuu, thats hard stuff for one beginner like me. but thank you!
hehe, you want fancy effects, then you gotta do fancy work. :o) Hope you will be curious enough to learn this part too.