- Home /
change the style of a gui by a key press
i am creating an inventory script that i want the user to press the numbers keys to interact with the bar along the bottom, but i cant get the images to change on key press only when they are clicked. i have tried using the toolbar but i get back errors, if someone knows a way in which to set a the toggle to active that would really help.
thanks in advance
function OnGUI() { var curIndex = 0; GUI.skin = customSkin; GUI.BeginGroup (new Rect (400, 500, 400, 120));
for(var item : System.Collections.Generic.KeyValuePair.<String, GameObject> in items) {
//iterate through all the items in the dictionary.
var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight);
var customStyle : GUIStyle;
var btnToggled : boolean;
var itemIndex : int;
itemIndex = curIndex;
customStyle = "icon_"+item.Key;
GUI.skin = customSkin;
btnToggled = GUI.Toggle(rect, btnToggled, curIndex+1+")", customStyle);
//Create a rect for the button. It is offset more for each item in the array.
if(Event.current.Equals (Event.KeyboardEvent (curIndex+1+""))) {
curItem = item;
//if the user clicks the button, select it as the current item.
}
if (curItem.Key == item.Key){
btnToggled = true;
}else{
}
curIndex++;
//increase the offset counter.
}
GUI.EndGroup();
}
A little confused... in your code, customStyle is a GUIStyle object.
But then you say...
customStyle = "icon_"+item.$$anonymous$$ey;
That line sets it as if it was a string. Wouldn't it be better to say
customStyle = customSkin.GetStyle("icon_"+item.$$anonymous$$ey);
This gets the named style from your custom skin and then uses it to draw the toggle. As it is, you are using a BLAN$$anonymous$$ custom style, as far as I can tell (and you are doing it before setting the GUI.skin property, too).
Thanks I've changed my script on what you said, but it still didn't solve the issue that I have which is, getting the custom style to change to active when the appropriate key was pressed. so when I press 1, 2, 3 or 4 the image will change to the active state of the style
Your answer
Follow this Question
Related Questions
GUI.Button is acting funky. 2 Answers
Is there a way of using just basic gui.Buttons as toggles? 1 Answer
GUI Button which looks like a Toggle 0 Answers
GUI Button Position - Can it float to the top-left? 3 Answers
Need help with toggle button 1 Answer