【问题标题】:Unity Changing GUI Colors / Slider Color / Label ColorUnity 更改 GUI 颜色/滑块颜色/标签颜色
【发布时间】:2016-12-02 00:47:54
【问题描述】:

我有一个用于小型模拟的设置脚本。

相机的背景颜色为黑色。当我有一个带有标签的 GUI.Horizo​​ntalSlider 时,我需要更改 Slidercolor。不然看不清楚。

谁能告诉我在哪里以及如何设置这些颜色? “ContentColor”、“BackgroundColor”不起作用..

我的代码:

 private int planetObjectsCount = 1;          // number of objects in the next scene
private int skyObjectsCount = 1;            // number of objects in the next scene
private int spaceObjectsCount = 1;          // number of objects in the next scene

private void OnGUI()
{
    GUI.color = SetGUIColor(); // Give GUI Elements a default Color

    planetObjectsCount = Mathf.RoundToInt(GUI.HorizontalSlider(GetGuiRect(Screen.width / 2, Screen.height * 2 / 8), planetObjectsCount, 1, 300));        // The Slider Element - store the value
    skyObjectsCount = Mathf.RoundToInt(GUI.HorizontalSlider(GetGuiRect(Screen.width / 2, Screen.height * 4 / 8), skyObjectsCount, 1, 100));             // The Slider Element - store the value
    spaceObjectsCount = Mathf.RoundToInt(GUI.HorizontalSlider(GetGuiRect(Screen.width / 2, Screen.height * 6 / 8), spaceObjectsCount, 1, 100));         // The Slider Element - store the value

    GUI.Label(GetGuiRect(Screen.width / 2, Screen.height * 3 / 8), "Objects on the Planet: " + planetObjectsCount.ToString());          // The Label for the Slider
    GUI.Label(GetGuiRect(Screen.width / 2, Screen.height * 5 / 8), "Objects in the Sky: " + skyObjectsCount.ToString());                // The Label for the Slider
    GUI.Label(GetGuiRect(Screen.width / 2, Screen.height * 7 / 8), "Objects in Space: " + spaceObjectsCount.ToString());                // The Label for the Slider

    if (GUI.Button(GetGuiRect(Screen.width * 0.85f, Screen.height / 2), "Build"))               // Menu Button
    {
        PlayerPrefs.SetInt("planetObjectsCount", planetObjectsCount);                   // Store the values to the PlayerPrefs
        PlayerPrefs.SetInt("skyObjectsCount", skyObjectsCount);                             // Store the values to the PlayerPrefs
        PlayerPrefs.SetInt("spaceObjectsCount", spaceObjectsCount);                     // Store the values to the PlayerPrefs
        LoadScene("Ingame");                                                                // Load the simulation
    }

    if (GUI.Button(GetGuiRect(Screen.width * 0.15f, Screen.height / 2), "Back"))        // Menu Button
    {
        LoadScene("MainMenu");                                                              // Back to MainMenu
    }
}

internal Rect GetGuiRect(float xPos, float yPos)                // Return a Rectangle for GUI Elements
{   
    float rectWidth = Screen.width / 5;
    float rectHeight = Screen.height / 10;

    xPos -= rectWidth / 2;
    yPos -= rectHeight / 2;

    return new Rect(xPos, yPos, rectWidth, rectHeight);
}

internal Color SetGUIColor()                                    // Set the GUI Color
{
    return Color.cyan;
}

【问题讨论】:

    标签: user-interface unity3d


    【解决方案1】:

    不太确定你想要什么。 但我相信你会通过使用GUIStyle 或GUISkin 来解决你的问题。 通过 GUIStyle,您可以更改许多属性,如您将使用的字体及其颜色。因此,您创建它并设置一个 GUI 组件来使用它。例如,在 GUILabel,您可以将 GUIStyle 作为第三个参数传递:

    public static void Label(Rect position, string text, GUIStyle style);
    

    而 GUISkin 是这些 GUIStyles 的集合,划分在所有 UI 组件中,这样您就可以创建一个全新的 UI 样式。

    但是,我认为您应该知道 Unity 还提供了一个完整的 UI system,您可以在不需要代码的情况下进行抽搐,坦率地说,这比使用 OnGUI 要好得多。举例来说,这里是 Unity 手册的链接,教你如何使用它创建 Slider 和 Label。

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多