1

我为我的菜单脚本编写了代码功能,该功能允许浏览构成菜单的按钮(通过使用 GUI.FocusControl() 将焦点从按钮更改为按钮)。但是,我对如何“激活”/“选择”当前关注的按钮感到困惑。理想情况下,当用户处于他或她希望选择的选项时,用户可以在菜单中上下导航并按下“输入”。任何建议都会很棒。

4

1 回答 1

0

我花了一段时间才弄清楚如何做到这一点,但最终我找到了解决方案。如果您仍在寻找答案,这就是我的做法。基本上你想利用 GUI 类的 SelctionGrid 函数。你可以在这里看看它是如何工作的,但这里有一些代码,其中包含你所要求的工作示例:

#pragma strict
@script ExecuteInEditMode;

var selGridInt : int = 0;    //This integer is the index of the button we are hovering above or have selected

var selStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];

private var maxButton : int;    // The total number of buttons in our grid

function Start()
{
    maxButton = selStrings.Length;    // Set the total no. of buttons to our String array size
}

function Update()
{
    // Get keyboard input and increase or decrease our grid integer
    if(Input.GetKeyUp(KeyCode.UpArrow))
    {
        // Here we want to create a wrap around effect by resetting the selGridInt if it exceeds the no. of buttons
        if(selGridInt > 0)
        {
            selGridInt--;
        }
        else
        {
            selGridInt = maxButton - 1;
        }
    }

    if(Input.GetKeyUp(KeyCode.DownArrow))
    {
        // Create the same wrap around effect as above but alter for down arrow
        if(selGridInt < (maxButton-1))
        {
            selGridInt++;
        }
        else
        {
            selGridInt = 0;
        }
    }

    if(Input.GetKeyUp(KeyCode.Return))
    {
        switch(selGridInt)
        {
            case 0: Debug.Log("Button "+(selGridInt + 1 )+ " pressed.");
                break;
            case 1: Debug.Log("Button "+(selGridInt + 1 )+" pressed.");
                break;
            case 2: Debug.Log("Button "+(selGridInt + 1 )+ " pressed.");
                break;
            case 3: Debug.Log("Button "+(selGridInt + 1 )+ " pressed.");
                break;
        }
    }
}

function OnGUI () 
{
    selGridInt = GUI.SelectionGrid (Rect (Screen.width/2-75, Screen.height/2-25, 150, 200), selGridInt, selStrings, 1);
}
于 2012-09-08T22:23:26.177 回答