【问题标题】:How to make GUI.Box corners rounded in Unity programmatically如何以编程方式在 Unity 中使 GUI.Box 圆角
【发布时间】:2019-05-23 17:21:28
【问题描述】:

我对 C# 和 Unity 了解不多。我只是遵循Vuforia's Unity Cloud Recognition tutorial 中的脚本,它以编程方式创建 GUI 框。所以我猜所有使用 Unity Inspector 的 stying 解决方案都不适合我。

我当前的 GUI.Box 样式

Texture2D texture = new Texture2D(1, 1);
texture.SetPixel(0,0,Color.white);
texture.Apply();

GUIStyle myBoxStyle2 = new GUIStyle(GUI.skin.box);
myBoxStyle2.fontSize = 40;
myBoxStyle2.normal.background = texture;
myBoxStyle2.normal.textColor = Color.black;
myBoxStyle2.alignment = TextAnchor.MiddleLeft;
GUI.Box (new Rect(Screen.width/4,Screen.height/6,Screen.width/2,Screen.height/8), mTargetMetadata, myBoxStyle2);

它看起来像这样(白框)

我检查了GUI Style Manual,没有帮助。

【问题讨论】:

  • 我很确定使用的图像背景的圆角。由于您使用的是纯白色纹理,边角较硬,因此您的边角会很硬。
  • @Draco18s 所以我应该改变纹理还是?我只想要一个白色背景和圆角,我不必使用Texture2D,使用任何其他方法都可以。
  • 您可以使用名为UISprite 的内置纹理,它有圆角(通常用于按钮)或buy something from the AssetStore。无论如何,您是否尝试过简单地不为它创造一种新风格? Afaik 默认情况下它还是使用圆形纹理?最后:你一定要使用GUI.Box吗?为什么不简单地使用 Unity 4 中引入的UI System
  • @derHugo 因为默认的背景是灰色的,这使得文本有点不清楚。谢谢,我试试Sprite 一个。

标签: c# unity3d


【解决方案1】:

圆角其实是GUI.Box的默认样式。

默认情况下,afaik 内部只使用UISprite 作为纹理。为了在您的 MonoBehaviour 组件上也使用它,您可以拥有一个

public Texture2D boxTexture;

并引用其中的 UISprite 以便将其用于您的样式。


但听起来你的问题实际上似乎相当

如何改变GUI.Box的颜色?

所以使用GUI你可以直接使用改变颜色

// store current values before changing
var color = GUI.color;
var contentColor = GUI.contentColor;

// change GUI colors
GUI.color = Color.white;
GUI.contentColor = Color.black;
{
    // draw Box with default style
    GUI.Box (new Rect(Screen.width/4,Screen.height/6,Screen.width/2,Screen.height/8), mTargetMetadata);
}
// reset GUI colors to former stored values
GUI.color = color;
GUI.contentColor = contentColor;

这应该已经解决了,或者您可以尝试做同样的事情,但使用GUI.backgroundColor 而不是GUI.color。并且只是为了使列表完整:您可以使用 GUI.contentColor 更改文本颜色。


但总的来说......我会说 Vuforia 给了你一个非常糟糕的选择。使用GUI 实际上可以追溯到 Unity 4.5。 (或多或少是 2015 年)。

它仍在使用,但实际上几乎用于Building Custom Inspectors 和其他编辑器脚本。

现在如 cmets 中所述,您应该使用 Unity 4.6 中引入的 "New" UI System

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    相关资源
    最近更新 更多