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)

OK further digging through old forum archives reveals that this is a pending issue. It is currently not possible to do this with scripting.

Damn, I really needed to be able to do this :/

Hey guys - check my overall reply below - hope it helps!

2 Answers

2

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.

Cheers!

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.

Thanks for the tip amirabiri, but I'd need to do this dynamically at runtime (I'd need just to read those borders, so I can set min size of GUITexture when I scale it), not in an editor script.

Yes I forgot that part... my needs were the same. You could try and "explore" the GuiTexture class with those classes or normal C# reflection and then try to do the same bypass with C# reflection or serialization.