How to make a texture fit on all objects

Hi! I am working on a 2D platformer.
I have multiple squares of different size which represent the “ground”. They all use the same texture but all of them look different. Check the image:

How can I do to make them all look the same like the one at the bottom ?

If you find the material component on the objects, it will have a couple of text boxes where you can set Tiling and Offset. for your issue you’ll want to make sure the Tiling is turned down low.

For your case I’d say try set tiling to something like x = 2 and y = 5, and see what it looks like, then just change them until it looks correct.

There is a hierarchy of solutions for this problem

  • Material for each object (cumbersome, lower performance)
  • Script that allows you to adjust the tiling (lower performance)
  • Script that automatically adjusts the tiling (lower performance)
  • UV mapping the textures in a modeling program (higher performance)
  • UV mapping textures in code (higher performance)
  • Shader solution (highest performance)

And there are a few other solutions out there as well. For a PC game, or a mobile game with just few object, lower performance solutions are unlikely to slow the game down. Here is a bit of code for the third solution. Attach the script to each object with the material. It takes the scaling from the x and y size of the object. If the ‘y’ size is near 0.0, then it uses x and z. The scaling is done at runtime. Note this is not a solution for 3D objects with non-uniform scaling.

#pragma strict

var factor = 1.0;

function Start() {
	var mf = GetComponent(MeshFilter);
	if (mf != null) {
		var bounds = mf.mesh.bounds;
		
		var size = Vector3.Scale(bounds.size, transform.localScale) * factor;
		
		if (size.y < .001)
			size.y = size.z;
		
		renderer.material.mainTextureScale = size;
	}
}

The ‘factor’ variable allows you to make individual adjustments on a object by object basis if you want to.