can we use same material and same mesh with different texture scaling in the project.....???????

Hi Everyone,
Sorry in advance if u dont understand my english well…

In my game… i am making platforms using one mesh (plane with 4 vertices) which is 1*1 (unity units) block and i have applied a material to it with tillable texture.

So suppose i want to make platform of 20:1 (width:height), for that i have two options:
1> Duplicate the mesh i said above 20 times.!!!
2> Scale Assets local scale to (20,1,0) and use materials texture scaling property and put Xscale to 20…!!!

the Obvious choice for me is to use 2nd option as it saves 19*4 vertices from rendering…!!!

Now if i want to make platform of 5:1 with the same material and same mesh??? the problem lies here…

if i use the same material from the first problem then the 5:1 platform texture looks like it is squeezed…
so if i change the texture scaling of the material in 5:1 platform it stratches the texture in the 20:1 platform…
so my question is>>>>>>>>>

can we use same material and same mesh with different texture scaling in the project…???

sorry if u dont understand it at first… but seriously this problem is getting into my nerves… i want to optimize my game…!!!

Well you need to use “another choice” if you want to use the same material. Modify the mesh of the other platforms and set their vertices to be bigger with uvs to match - this is a lot less hard than it sounds. Then when you use the texture it will tile because you have tiling turned on and then tile again because the uvs of your new mesh are greater than 1.

You have two choices around doing this:

  • Do it at runtime based on some properties in a script that you attach to the platform
  • Do it in the editor and save the updated mesh as an asset or modify the attached mesh but make sure that it is a copy unique to this platform.

Your planes are simple - so there will be 6 vertices and 6 uvs to update (2 triangles to make 1 plane). If you make the platform 20 world units long then just modify the vertices and make the uvs match the 2d version of the vertex points. So if you make the uv of a point (20,0) it will tile 20 times before reaching that world point. A square plane 20 wide might then look like this for the vertices and uvs (just for an example - it would be easier to construct the array by code):

  var vertices = new {} [
   new Vector3(0,0,0),
   new Vector3(20,0,0),
   new Vector3(0,20,0),
   new Vector3(20,0,0),
   new Vector3(20,20,0),
   new Vector3(0,20,0)

  var uvs = new {} [
   new Vector2(0,0),
   new Vector2(20,0),
   new Vector2(0,20),
   new Vector2(20,0),
   new Vector2(20,20),
   new Vector2(0,20)

It is actually not as hard as it sounds.

Here is the documentation on vertices and uvs. You will use the platforms MeshFilter.mesh to access the mesh data. Changing the sharedMesh changes it for ALL objects - so operating on mesh is safer.