Repeating GUI Background Image

Hi,

I am creating a script that will generate and display GUI overlays on-demand using a given piece of text as the content.

The aim of the game here is to produce a method which can just take a given text and draw a box on the screen containing the text with the appropriate style. All of the boxes drawn must have a border consisting of images for the corners and sides. Since the text is highly variable and generated at run time, the box borders must be scalable.

There is a function, makeOverlay(string txt), that causes a GUI Box to be drawn on screen (with txt in), surrounded by 8 “border” Boxes. The border boxes each have an image assigned for the background:

((GUIStyle)tl).normal.background = (Texture2D)Resources.Load(“GUI/Dialog/Back/Back Top”);
tl.normal.background.wrapMode = TextureWrapMode.Repeat;
GUILayout.Box(“”,tl);

The problem I am having is that I can’t get the background images to repeat in the x and y directions for the boxes that appear on the sides (centre-top,middle-left,…). I was aiming to implement this like you would use CSS to create a similar effect with

background-repeat:repeat-x;

Any help making this work or any suggestions for a more sensible approach to making an overlay generator would be very much appreciated.

Cheers,
Tom

P.S. Sorry if this is a repeat post but I am having trouble finding it anywhere…

UnityGUI currently does not support that. Your best bet is to make a loop where you draw multiple boxes, using GUI.BeginGroup to handle your clipping.

Cheers Nicholas,

I have managed to get the effect I was looking for:

GUIStyleState bms = new GUIStyleState();
bms.background = (Texture2D)Resources.Load(“GUI/Dialog/Back/Back Bottom”);
bm = new GUIStyle(style);
bm.normal = bms;
bm.fixedHeight = corner_dims.y;
bm.stretchWidth = true;
bm.normal.background.wrapMode = TextureWrapMode.Repeat;

Then…
// Bottom
GUILayout.BeginHorizontal();
GUILayout.Box(“”,bl);
GUILayout.Box(“”,bm);
GUILayout.Box(“”,br);
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndArea ();

This appears to produce the correct effect… I had a problem importing my textures before which was causing me to see bad output and assume it was the code…

Thanks,
Tom

Hi again,

I am now having trouble with getting the dimensions to work so that only the minimum dimensions are used to display the given content.

I need to be able to position my group of GUILayout elements at a variable position on the screen. But if I use BeginArea or BeginGroup, they require a Rect, which includes a width and height. This then results in the GUILayout objects trying to fill the area and large areas of whitespace in my box are the result.

Any help/suggestions?

Cheers,
Tom