GUISkin: Tiling instead of stretching textures?

I am specifying a GUISkin for my GUI layout, and I need to know if there is a way to make textures "tile" instead of stretch. This results in horrible looking windows as they get larger because of the texture filtering.

Anyone know how to do this?

You can do this by using the ‘GUI.DrawTextureWithTexCoords’ function instead

Le code:

backgroundTexture.wrapMode = TextureWrapMode.Repeat;
GUI.DrawTextureWithTexCoords(screenBounds, black, new Rect(0, 0, screenBounds.width / backgroundTexture.width, screenBounds.height / backgroundTexture.height));

Essentially just ensure that that texture you’re using has a repeat wrap mode, then just use the dimensions of the texture to divide the screen height and width into UV positions

If you use a GUI Skin, you can fake a tiling GUI by adjusting the border, margin, and overflow properties of something that is a background element. Alternatively, if you are using scripting, you could do some screen width and height calculations in start and create a new texture then SetPixels based off the original texture. Make sure the texture you want to tile is set to "Readable" on the import settings. If you need a sample of code (javascript) to get you started on this solution, here's where I'd start (javascript) as of now:

var GuiBackgroundTexture : Texture2D;
private var tiledTexture : Texture2D;
private var GUISize : Rect;

function Start()
{
   GUISize = new Rect(0, 0, Screen.width, Screen.height);
   tiledTexture = new Texture2D(GUISize.width, GUISize.height);
   var i : int;
   var n : int;
   for(i = 0; i < tiledTexture.width; i++)
   {
      for (n = 0; n < tiledTexture.height; n++)
      {
         var pixCol = GuiBackgroundTexture.GetPixel(i % GuiBackgroundTexture.width, n % GuiBackgroundTexture.height);
         tiledTexture.SetPixel(i, n, pixCol); 
      }
   }
   tiledTexture.Apply();
}

function OnGUI()
{
    GUI.DrawTexture(GUISize, tiledTexture);
}

EDIT: In Unity3 you can make a texture readable by going to the texture import settings, selecting "Advanced" from the dropdown, and making sure "Read/Write Enabled" is checked.

Just a little function I created which works very nicely. Feel free to re-use. :)

private void TileTexture(Texture texture, Rect tile, Rect areaToFill, ScaleMode scaleMode) 
{
    // Tiles an <areaToFill> with a <texture> which is scaled to the 
    // size of a <tile> using a given <scaleMode>.
    // Author: Isaac Manning Dart

    for (float y = areaToFill.y; y < areaToFill.y + areaToFill.height; y = y + tile.height) {
        for (float x = areaToFill.x; x < areaToFill.x + areaToFill.width; x = x + tile.width) {
            tile.x = x; tile.y = y;
            GUI.DrawTexture(tile, texture, scaleMode);
        }
    }
}

There is no way that I know of to be able to tile a texture for GUI use, most people use 9-slicing. 9-slicing enables you to define how the GUI element should look like at the top,bottom,right,left and middle.

Take a look at this page in the unity manual: http://unity3d.com/support/documentation/Components/gui-Customization.html

This page: http://unity3d.com/support/documentation/Components/class-GUISkin.html

And this page: http://unity3d.com/support/documentation/Components/class-GUIStyle.html Look for the Border settings.

They mostly say the same thing but a bit differen. There is a page which shows how 9-slicing works but I can't find it right now.

I manage to have this behavior by using a wide background. At least on width... Can't get it on height.