2012-08-25 55 views
1

我有我的菜單腳本的編碼功能,允許組成菜單的按鈕通過導航(通過使用GUI.FocusControl()更改焦點從按鈕到按鈕)。但是,我很困惑如何「激活」/「選擇」當前關注的按鈕。理想情況下,用戶可以上下瀏覽菜單,並在用戶選擇他或她想要的選項時按下「輸入」。任何建議都會很棒。Unity3D圖形用戶界面鍵盤導航

+0

的統一標記是微軟統一。請不要濫用它。 –

回答

0

我花了一段時間才弄清楚如何做到這一點,但最終我找到了解決方案。如果你仍然在尋找答案,那麼我就是這樣做的。基本上你想使用GUI類的SelctionGrid函數。你可以把一個峯值here,看看它是如何工作,但這裏的一些代碼的你所要求的工作例如:

#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); 
}