2016-12-01 275 views
1

我有一個小模擬的設置腳本。Unity改變GUI顏色/滑塊顏色/標籤顏色

相機的BackgroundColor是黑色的。當我有一個帶有標籤的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; 
} 

回答

0

不太清楚你想要什麼。 但我相信你會通過使用GUIStyleGUISkin來解決你的問題。 通過GUIStyle,您可以更改許多特性,如您將使用的字體及其顏色。所以你創建它並設置一個GUI組件來使用它。在GUILabel,例如,你可以通過GUIStyle作爲第三個參數:

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

而且GUISkin是這些GUIStyles在所有的UI組件劃分的集合,這樣就可以創建一個全新的UI風格。

但是,我認爲你應該知道Unity也提供了一個完整的UI system,你可以在不需要代碼的情況下抽搐,坦率地說,使用OnGUI會更好。爲了舉例說明,下面是Unity手冊的教學鏈接,教程如何創建SliderLabel