首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Unity更改GUI颜色/滑块颜色/标签颜色

Unity更改GUI颜色/滑块颜色/标签颜色
EN

Stack Overflow用户
提问于 2016-12-01 23:03:03
回答 1查看 898关注 0票数 1

我有一个用于小型模拟的Settingsscript。

摄像机的BackgroundColor为黑色。当我有一个带有标签的GUI.HorizontalSlider时,我需要改变滑块的颜色。否则你就看不清楚了。

有人能告诉我在哪里以及如何设置这些颜色吗?"ContentColor“、"BackgroundColor”不工作..

我的代码:

代码语言:javascript
运行
复制
 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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-12-02 00:47:55

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

代码语言:javascript
运行
复制
public static void Label(Rect position, string text, GUIStyle style);

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

为了举例说明,这里是Unity手册的链接,教你如何用它创建SliderLabel

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40913908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档