I have a series of GUILayout.Buttons with a string and a reference to a property in the GUI skin so I can align the buttons and whatnot.
What I want to do is specify in code what background texture each button should have, as each button should display a unique icon and I don’t particularly fancy having lots of different GUI button style just to change the background texture for each button type.
So how should I modify the code to insert a GUITexture as a background texture for the button? — Here’s a little insight into what I’m specifically doing. A button is drawn via a loop which cycles through items in an inventory. From each item it will grab the name and display it on the button. What I want is to pull an ‘icon’ property from the inventory and display it as a background texture inside the button.
Typically rather than changing the button background you’d change the texture/icon displayed on top of it. That way the buttons all look similar and have similar mouse-overs, etc. and you don’t have to make normal/active/hover versions of all the inventory icons. Just pass GUIContent to the GUILayout functions instead of a string – GUIContent has a string and an icon (and another string for a tooltip).
In that case it would just be:
GUILayout.Button(
new GUIContent( theInv.CurrentItems_.Name, theInv.CurrentItems*.Icon ),*_
“InvButton”); If you absolutely want the backgrounds to change, then you could copy the style in code, and assign the icons as you went through. e.g. GUIStyle styleInv = new GUIStyle( GUI.skin.GetStyle(“InvButton”) ); // copy it for( … ) { styleInv.normal.background = theInv.CurrentItems*.Icon;* GUILayout.Button(theInv.CurrentItems*.Name, styleInv );* } But I’d do it the first way (with the GUIContent).