Tile texture on a GUI

I’d like to use a small image as a background texture for my GUI, but I don’t know how to tell Unity to tile it so that it does not get stretched when the screen gets bigger than its maximum size. Can anybody help me, please?

What are you using to draw the texture as a “background”? A Label? Are you using a Window? Other?

I’m just using
GUI.DrawTexture(new Rect(0,0,Screen.width, Screen.height), back);

I’ve also tried the various ScaleMode possibilities, but they all stretch the figure…
I’m writing in C#, by the way. Thanks for helping.

Instead of using DrawTexture which can only use the non-tiling stretch modes (AFAIK), you can use a label with a custom GUIStyle so it (a) uses your image as the background, and then (b) set the border values to large numbers to get it to tile. That will take some math fun to figure out exactly what border settings to use given your texture size and the monitor width, but it’s at least possible (if wonky).

Sorry but I just don’t have anything more elegant to offer, hopefully someone else can chime in and clue us both in if there’s a better way. :slight_smile:

This would be easier:

void DrawTiled (Rect rect, Texture tex)
{
	GUI.BeginGroup(rect);
	{
		int width = Mathf.RoundToInt(rect.width);
		int height = Mathf.RoundToInt(rect.height);
		
		for (int y = 0; y < height; y += tex.height)
		{
			for (int x = 0; x < width; x += tex.width)
			{
				GUI.DrawTexture(new Rect(x, y, tex.width, tex.height), tex);
			}
		}
	}
	GUI.EndGroup();
}

The above solution would lead to enormous amount of draw calls.

PiA is right, I recently looked up on how to do this and it doesn’t seem there’s an “easy” + optimal way. The best I can think of is doing the following:

Once only:

Every frame:

  • Render the dynamically created texture

The upside is that you can do the whole kaboodle in one draw call, but at the sacrifice of eating up a bit more memory to keep the dynamically created texture around.