- Home /
C# - Setting color of a rect on creation
Rect rightArrowRect = new Rect(Screen.width*1/2,0,Screen.width,Screen.height/6);
Rect leftArrowRect = new Rect(0,0,Screen.width*1/2,Screen.height/6);
Rect upArrowRect = new Rect(0,Screen.height/6,Screen.width,Screen.height/6);
For my interface, I am naming 3 rectangles as gui buttons. As of now, they are clear. How can I set the rectangle's color on creation for each element? Everything I have read so far has to do with GUI something or others... Apologies for the lack of details, C# isn't a code that I'm fairly familiar with.
Bonus: is there a way to set a small texture image to the center of each rect to make them easier to identify?
Answer by flaviusxvii · Dec 05, 2013 at 08:12 PM
A Rect isn't actually a renderable thing. You want something derived from http://docs.unity3d.com/Documentation/ScriptReference/GUIElement.html
like http://docs.unity3d.com/Documentation/ScriptReference/GUITexture.html perhaps.
Ah, didn't know that. Now just to figure out how all this stuff works...
Answer by stevethorne · Dec 05, 2013 at 08:13 PM
The Rect structure does not contain information for color, this is something you're going to have to change through the GUI buttons. When you call your GUI button you can apply the color you want through the GUI.backgroundColor property.
GUI.backgroundColor = Color.red;
GUI.Button( rect, "red button", guiStyle );
GUI.backgroundColor = Color.blue;
GUI.Button( rect, "blue button", guiStyle );
You can also do this through GUIStyles, which can be better if you want to do more with the style of the button, but will require more work.
Your answer
Follow this Question
Related Questions
Fade in/out continously 1 Answer
Rect Following Object : Unity3d 1 Answer
Setting a Rect from another RectTransform 0 Answers
GUI label dimensions bug 2 Answers
Need help understanding GUI and Rect.Contains interaction 0 Answers