问题
I would like to show a transparent GUI.Box on screen with a colored border. How can I do this?
回答1:
- Create a PNG image with a transparent center and a border with your desired thickness. The size of the image doesn't matter, but the border around the image should be your desired border size. For instance, I created the 64 x 64 image with a 2px green border on Pixilart.
Add the PNG as a 2D texture in your project. You can drag the image from Finder into your Assets pane to do this.
Use the following GUI.Box invocation:
public Texture2D BoxBorder; // Set this to your border texture in the Unity Editor
void OnGUI()
{
var borderSize = 2; // Border size in pixels
var style = new GUIStyle();
//Initialize RectOffset object
style.border = new RectOffset(borderSize, borderSize, borderSize, borderSize);
style.normal.background = BoxBorder;
GUI.Box(new Rect(/* rect position */), GUIContent.none, style);
}
来源:https://stackoverflow.com/questions/62224353/how-to-show-an-outlined-gui-box-in-unity