Question regarding GUI Background repeat solution

Hey everybody,
I am working on the Unity Engine plugin and was having some trouble repeating the GUI background. After some research apparently there isn’t an option for that, so I had to do some workaround.

This is what I came up with:

public static void DrawBGInRect(Rect rect, Texture2D texture)
{
    GUI.BeginScrollView(rect, Vector2.zero, rect, false, false, GUIStyle.none, GUIStyle.none);
    GUIStyle style = new GUIStyle();
    style.normal.background = texture;

    float size = texture.width;
    float lastXPos = rect.x;
    float lastYPos = rect.y;

    Rect tileRect = new Rect();
    bool reachedEnd = false;
    do
    {
        tileRect = new Rect(lastXPos, lastYPos, size, size);

        GUI.Label(tileRect, GUIContent.none, style);

        lastXPos += size;
        if (lastXPos >= rect.width)
        {
            lastYPos += size;
            if (lastYPos >= rect.height)
            {
                reachedEnd = true;
            }
            lastXPos = rect.x;
        }
    }
    while (!reachedEnd);
    GUI.EndScrollView();
}

Really easy and simple solution, but it does the job, tho I was curious if there are any other better solutions to this problem. Does anyone have any ideas on this topic? Thanks in advance

Looks fine to me. I’d probably just use that approach if it works!

The only other way that occurs to me would be to have a RenderTexture that you render your repeating pattern to at the size/scale you need it, then render that one texture with a single call.

1 Like