按下一个按钮,聚焦所有按钮

本文关键字:按钮 聚焦 下一个 | 更新日期: 2023-09-27 17:54:59

我正在尝试在unity3d中实现库存系统。有几种方法可以在gui上实现库存,但我选择了最简单的方法。

gui上的每个项目都表示为一个单元格(gui . button)。每个空单元格意味着库存(GUI.Box)中的空位置。当我按下一个按钮选择一个项目时,所有的按钮都集中起来了。

我的问题是为什么会发生这种情况,以及如何修复,使只有一个按钮集中?我基于空的GUISkin创建了一个GUISkin。谢谢您的关注

下面是显示库存的代码。

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...
}

按下一个按钮,聚焦所有按钮

GUI.color = Color.black;

使每个按钮的alpha颜色变高