How to show an outlined GUI Box in Unity?

回眸只為那壹抹淺笑 提交于 2021-01-02 04:17:12

问题


I would like to show a transparent GUI.Box on screen with a colored border. How can I do this?


回答1:


  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.

  1. Add the PNG as a 2D texture in your project. You can drag the image from Finder into your Assets pane to do this.

  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!