Set/Modify gui texture border from script

Is there a way to set the border properties of a GuiTexture object from script?

I’ve gone over the available properties several times but I can’t seem to find the corresponding properties. Did I miss it or is it just not supported for some reason?

(I’m not referring to the pixel inset btw which I can see, but the “Left Border”, “Right Border”, … etc)

Hey all;

I found myself with this exact same problem, after digging and banging my head against the keyboard for a 'lil bit I was actually able to find the Script Reference article for the RectOffset.

According to this, the RectOffset can just be defined new with specific values given through four (rect-like) parameters.

So if you have a texture that you’d like to adjust the border on-the-fly, then just create a temporary RectOffset, assign the appropriate values for left,right,top,bottom - then just set the border of your texture to the tempOffset you just created; like so:

RectOffset tempOffset = new RectOffset(leftValue, rightValue, topValue, bottomValue);

gameObject.GetComponent<GUITexture>().border = tempOffset;

By doing so - you will change the border size at run-time, and can scale your pixel borders if needed.

I sincerely hope that this helps someone else out more quickly than I was able to find a solution.

Keep in mind, this doesn’t affect the visual WIDTH or HEIGHT of the GUITexture, it is merely adjusting the pixels that are not stretched as the texture is.


I actually had an idea on how to do this after all which I didn’t get around to testing yet so someone else might want to try it:

Checkout SerializedObject and SerializedProperty. You might be able to write a wrapper or utility method that bypasses this limitation and allows you to set the properties from code.