Gui texture tiled

Hello!

Does anybody knows how to make a GUI Texture tiled?

I know in materials we cna just use:

renderer.material.SetTextureOffset (“_MainTex”, offset);
renderer.material.SetTextureScale (“_MainTex”, size);

But how about GUI Textures?

Does anyone knows?

Best regards,
topastop

If you want to tile an image, just decide how big the image will be, and how many tiles you want, loop through the positions, and draw them using DrawTexture.

// C# Example
public Vector2 tilePosition;
public Texture image;
public float tileSize;
public int tilesWide, tilesHigh;
void OnGUI()
{
   for (int y = 0; y < tilesHigh; y++)
      for (int x = 0; x < tilesWide; x++)
         GUI.DrawTexture(new Rect(tilePosition.x + (x * tileSize), tilePosition.y + (y * tileSize), tileSize, tileSize), image);
}

If you want to use a texture atlas, then you would use DrawTextureWithTexCoords instead of DrawTexture, and you would probably need to have a 2D Array of values indicating which part of the atlas to draw at a given position.

Do you know if it is possible to do using guiTexture.texture ?

A GUITexture object can’t be tiled, so you have to do it some other way.

–Eric

Best and simplest way to do it is this:

GUI.DrawTextureWithTexCoords (itemRect, texture, new Rect (0, 0,width,));

This thread helped a lot. Might I add, I used the following tweak to make texture tile all the way across the screen automatically. I made tilesWide private and computer how many it would take to go across the screen:

tilesWide = Screen.width / tileSize +1;

the +1 is to make sure the texture doesn’t get cut off.

Yeah, tiling is such a great feature (it’s been around since Unity 3.5 as I remember, but I found about it much later).

I played with it a lot.