2017-12-18 193 views
0

我需要一種方法來檢查是否按下了一個UI按鈕,點擊事件並不真正有用,因爲Input方法(稱爲蝕刻時間是其玩家輪到的時間)將值返回給主函數while循環將完全停止遊戲,並且只有在玩家轉向時(玩家轉而Method正在等待輸入時)纔會接受輸入,由於這些原因,Unity事件觸發器似乎不會像一個可用的選項。所有我需要的是一種方法來檢查按鈕的狀態。檢查是否按下了用戶界面按鈕或替代選項Unity

注意:一個對象作爲我的主要方法 的IM期運用的start()方法,如果有應該與任何問題,讓我知道

另請注意:我的遊戲轉移到Unity所以我想改變最小的變化輸入+輸出方法的代碼

//TurnInput is an array of bools tracking witch buttons are being pressed 
//(9 buttons) 
private Block[] PlayerTurn(Block[] grid) 
{ 
    TurnNotDone = false; 
    while (!TurnNotDone) 
    { 
     //gets stuck unity crash 
     //needs to wait until player click on one of the buttons 
     //(when player click on a button is turn is over and the turn is 
     //passed to the AI) 
    } 
    for (int i = 0; i < 9; i++) 
    { 
     if (TurnInput[i]) grid[i] = SetBlock("O"); 
    } 
    return grid; 
} 
//trigger by event trigger on button gets an int for the button Index 
public void PointerDown (int i) 
{ 
    TurnInput[i] = true; 

} 
//trigger by event trigger on button gets an int for the button Index 
public void PointerUp(int i) 
{ 
    TurnInput[i] = false; 
} 
+0

如果Unity事件觸發器不是可用的選項 - 您的代碼結構就會混亂。你能否提供一段代碼來清除你想達到的目標? –

+0

另一種方法是:1.檢查鼠標按鈕是否被點擊,2.將屏幕翻譯到世界位置3. raycast 4.檢查按鈕是否被raycast命中。 –

+2

重塑遊戲統一,多數民衆贊成我的意見 – Lestat

回答

2

也許你可以使用,而不是while循環協同程序:

  1. gameloop協同程序「停止」,以等待用戶輸入而playerTurn是真的
  2. ButtonClicked事件辦理了設置playerTurn爲假(添加ButtonClicked方法UI按鈕的OnClick事件處理程序)
  3. AI的回合
  4. 集playerTurn到實現再
  5. 轉到1

小例子:

public class MinimalExample : MonoBehaviour { 

public struct Block { 
    public bool isOBlock; 
} 

bool playerTurn; 
Block[] grid; 
bool[] TurnInput; 

// Use this for initialization 
void Start() { 
    grid = new Block[9]; 
    TurnInput = new bool[9]; 
    StartCoroutine (GameLoop()); 
} 

// GameLoop 
IEnumerator GameLoop() { 
    while (true) { 
     yield return new WaitWhile (() => playerTurn == true); 
     for (int i = 0; i < 9; i++) { 
      if (TurnInput[i]) grid[i] = SetBlock("O"); 
     } 
     Debug.Log ("AI here"); 
     playerTurn = true; 
    } 
} 

Block SetBlock(string s) { 
    var r = new Block(); 
    r.isOBlock = (s == "O"); 
    return r; 
} 

//trigger by event trigger on button gets an int for the button Index 
public void ButtonClicked (int i) { 
    TurnInput[i] = true; 
    playerTurn = false; 
} 

} 
+0

當然,我已經實現了所有其他方法,但似乎如果它在Unity中工作,它將成爲我的問題的完美解決方案。謝謝 !我會檢查它是否正常工作 – toto

+0

確實工作謝謝你 – toto

相關問題