Tiling a material using game units

Hey,

I’m trying to figure out if there’s some way to make a material tile a texture using game units instead of tiling it based on the size of the object.

For example, in the image below, there are 2 quads using the same material, but the material doesn’t look the same on both of them because the quad on the right is larger than the one on the left:

[16227-material+example.png|16227]

So, the tiling settings in the material mean if it’s set to 1x1, it’ll put the texture across the whole surface once; if it’s set to 2x2, it’ll put the texture across the whole surface 4 times. If the surface is huge, the texture will be a lot more visible; if the surface is tiny, the texture will be shrunken.

Is there any way to make it tile the texture based on game units?
For example, if it was set to 1x1 tiling, then it could tile it 4 times per game unit, so if I had a quad with 4 units of size, it’d tile 4 times, not just once across the whole quad.

Sorry if I’ve typed up a storm and made this more difficult to understand than it has to be; if I haven’t explained enough, please let me know. Thanks for reading this far, and thank you for your time!

You can set the texture scale at runtime. I think that is what @sethuraj is suggesting:

renderer.material.SetTextureScale ("_MainTex", transform.localScale);

Note this line of code assumes that your shader is using _MainTex as the texture (most do). It is also assuming that you are basing your texture on the X and Y of the local scale of the object (true of Quad). Wouldn’t work for a Plane for example, you would need instead:

renderer.material.SetTextureScale ("_MainTex", Vector2(transform.localScale.x, transform.localScale.z));

Note setting the local scale breaks batching, so if you are going to do this for many game object, you might want to look for an alternate solution.