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?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
18 People are following this question.
Texture Tiling Stopped Working in Unity 4?
Tiling material with multiple textures.
Image loaded with LoadImageIntoTexture gets inverted
Texture is Tied to Two Cubes
Error creating a material ?