检查是否按下了用户界面按钮或替代选项Unity

我需要一种方法来检查是否按下了一个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;

}

回答:

也许你可以使用,而不是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;

}

}

以上是 检查是否按下了用户界面按钮或替代选项Unity 的全部内容, 来源链接: utcz.com/qa/262983.html

回到顶部