How to fill the entire GUI.Box with a texture and keep rounded edges?

If I create a GUI.Box using a texture in the constructor, the box has rounded edges but the texture does not fully occupy the space.

If I use a GUIStyle with the box and set its background to my texture, the entire box is filled but the rounded edges are gone.

How would I fill the entire box and keep the rounded edges?

Thanks.

When you draw a GUI.Box, the texture is displayed ON the box, much as it would inside of a button. That's why it doesn't fill the box. The rounded edges are presumably coming from the default GUIStyle.

So the normal solution is to paint the rounded edges directly in your texture using a graphics utility. Then, you could either use GUI.Box and make your texture the background, like you described, or I believe you could just use it as the content argument to GUI.Box or GUI.Label, and use a GUIStyle that has no background texture specified.

If you have a lot of textures to add rounded corners to, some graphics programs are scriptable; you could write something that would take a folder of textures and save them as new textures with rounded corners. Googling it, I see found lots of tutorials/scripts for Photoshop, Gimp, Quark. I'm sure there are others.

But let's say that for some reason you need it to happen at run-time, rather than draw rounded corners on your textures beforehand. There may be a way. Here are some ideas, though none of these are easy, built-in solutions.

  1. It's very possible that third-party tools like EZGUI have ways to do this.

  2. You could do it at runtime in Unity yourself, perhaps using Texture2D.GetPixel(s) and Texture2D.SetPixel(s) to make the corners invisible (adjusting the alpha mask) after the textures are first loaded. (I've never seen anyone do this, but its theoretically possible.)

  3. If you assigned the texture to a Plane object instead, you could use a shader that includes a mask to cut off the corners. Downside is, you'd have to place and scale the 3D objects to make it look like a 2D GUI. That isn't so easy to do yourself- utilities like SpriteManager were invented to solve that problem.