我正在使用C#为Unity 2017制作一个用于Android的Tetris克隆。我按照Youtube教程,完成了项目。然而,我想用屏幕按钮移动产生的Tetrominos,而不是使用触摸控件。有没有办法可以使用按钮访问currentActiveTetrimino上的脚本来控制它的移动?
此代码附加到所有tetrominos,它们是随机生成的,我不知道如何让按钮能够访问它,所以我可以移动它们。
void CheckUserInput () {
if (Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.LeftArrow)) {
movedImmediateHorizontal = false;
horizontalTimer = 0;
buttonDownWaitTimerHorizontal = 0;
}
if (Input.GetKeyUp (KeyCode.DownArrow)) {
movedImmediateVertical = false;
verticalTimer = 0;
buttonDownWaitTimerVertical = 0;
}
if (Input.GetKey (KeyCode.RightArrow)) {
MoveRight ();
}
if (Input.GetKey (KeyCode.LeftArrow)) {
MoveLeft ();
}
if (Input.GetKeyDown (KeyCode.UpArrow)) {
Rotate ();
}
if (Input.GetKey (KeyCode.DownArrow) || Time.time - fall >= fallSpeed) {
MoveDown ();
}
if (Input.GetKeyUp (KeyCode.Space)) {
SlamDown ();
}
}
public void MoveLeft () {
if (movedImmediateHorizontal) {
if (buttonDownWaitTimerHorizontal < buttonDownWaitMax) {
buttonDownWaitTimerHorizontal += Time.deltaTime;
return;
}
if (horizontalTimer < continuousHorizontalSpeed) {
horizontalTimer += Time.deltaTime;
return;
}
}
if (!movedImmediateHorizontal) {
movedImmediateHorizontal = true;
}
horizontalTimer = 0;
transform.position += new Vector3 (-1, 0, 0);
if (CheckIsValidPosition ()) {
FindObjectOfType<Game> ().UpdateGrid (this);
} else {
transform.position += new Vector3 (1, 0, 0);
}
}