- Home /
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:
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!
try normalizing the tiling value to object scaling.I didnt try this but try something like this (UVTiling/Localscale)*FixedScaleVal
Answer by robertbu · Oct 07, 2013 at 04:35 PM
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.
Thanks for the answer. That works great for planes and quads, but what about with a customized shape, like a level? For example, if you had a wall that twists and turns at various locations and you wanted to put a texture across it, how would you apply your code to that situation?
Your answer
![](https://koobas.hobune.stream/wayback/20220613115444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Texture is Tied to Two Cubes 1 Answer
Real Sized Texture Tiling 2 Answers
Multiple random tiled textures in one material 1 Answer
Tiling material with multiple textures. 1 Answer
Image loaded with LoadImageIntoTexture gets inverted 0 Answers