Press one button, focus all buttons
NickName:Rinomancer Ask DateTime:2015-06-11T06:13:44

Press one button, focus all buttons

I am trying to implement an inventory system in unity3d. They are several ways to realize an inventory on gui but i've chosen the easiest.

Every item on gui is represented as a cell(GUI.Button). Every empty cell means empty place in inventory(GUI.Box). When I press one button to chose an item all buttons are getting focused.

My question is why it happens and what to fix to make only one button to be focused? I created a GUISkin based on empty one. Thank you for attention

Here is the code to show an inventory.

void OnGUI(){
    if (visible) {
        GUI.skin = skin;

        GUI.Window(0,new Rect((Screen.width-1024)/2,0,1024,600),InventoryBody,"Inventory");
    }
}

void InventoryBody(int id){
    GUIStyle style = new GUIStyle();

    if (currentItem) {
        GUI.DrawTexture (new Rect(550f,70f,80f,80f), currentItem.texture);
        GUI.color = Color.red;


        GUI.Label (new Rect(700f,50f,400f,300f), currentItem.name);

        GUI.color = Color.black;

        string desc = "Description: "+currentItem.description;

        style.wordWrap=true;
        style.fontSize=18;

        GUI.Label (new Rect(650f,100f,300f,500f), desc,style);


        if(GUI.Button(new Rect(700f, 290f, 150f,50f), "Cancel")) {
            currentItem = null;

        }


        if(GUI.Button(new Rect(700f, 230f, 150f,50f), "Use")) {
            currentItem.Use ();
        }
    }

    //1st column
    GUILayout.BeginArea (new Rect (80f,60f,600f,600f));

    for (int i = 0; i<5; i++) {
        if(items[i]!=null){

            if(GUILayout.Button (items [i].texture, GUILayout.Width (80f), GUILayout.Height (80f)) ){
                currentItem = items[i];
            }


        } else {
            GUILayout.Box("", GUILayout.Width(80f),GUILayout.Height(80f));
        }
    }

    GUILayout.EndArea ();

    //2nd column
    GUILayout.BeginAr...
}

Copyright Notice:Content Author:「Rinomancer」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30768232/press-one-button-focus-all-buttons

More about “Press one button, focus all buttons” related questions

Press one button, focus all buttons

I am trying to implement an inventory system in unity3d. They are several ways to realize an inventory on gui but i've chosen the easiest. Every item on gui is represented as a cell(GUI.Button). ...

Show Detail

Trying to get focus on all buttons in a QButtonGroup

I am trying to get focus on all the QPushButtons in QButtonGroup. I am installing eventFilter on all buttons added in the button group and capturing the Tab key press event on each button and shift...

Show Detail

Press tab to focus on top level sections ignoring all focusable children

Consider that you have several focusable HTML sections, e.g. forms. Each section contains random number of focusable elements, e.g. inputs, links, buttons. Question Is there a generic way to make o...

Show Detail

java: Trigger KeyEvent without focus on button or apply focus on multiple buttons to trigger KeyEvent

I'm making a drum app which has buttons that serves as my drums. What I want is that the buttons will trigger upon key press so the user can play drums with the keyboard. I'm using the keyTyped ev...

Show Detail

Buttons keeping their focus class after losing focus

I have some buttons in a bottom toolbar of a gridpanel that control adding, and removing records from the row-editing grid. The handlers are pretty simple: "new" button creates an instance of the ...

Show Detail

Change Focus after button press

I have a simple button that calls a routine onClick. I use document.getElementById('Start').disabled=true; to disable the button so it can not be pressed again. On most things there is no problem

Show Detail

How to toggle focus between buttons without losing focus on clicking button

Im doing speed game where Thread changes focus on 4 different buttons. I want only the thread to change focuses on the buttons. The problem I am having is that when the thread for example chooses b...

Show Detail

Focus issue while tab press

I have three buttons Cancel, Submit &amp; Reset in a form and a close button in the titlebar. When I press tab button it focuses close button of the form, next press switch the focus to the first b...

Show Detail

imacros puts focus on button, but will not press it

FF 42.0 / Imacros for FF BUILD=8940826 / 2.8Ghz 8 core Early 2008 MacPro / OSX 10.8.2 This seems like it should be so simple. I just want to hit the search button and enter a search term. TAB T=...

Show Detail

Stop buttons getting focus

I'm implementing a text formatting toolbar using the following buttons. https://jqueryui.com/button/#checkbox The problem is that I simply loose the focus on the text inputs being edited when I p...

Show Detail