【问题标题】:Unity Coroutine for gameMenu用于游戏菜单的 Unity 协程
【发布时间】:2016-12-08 14:49:29
【问题描述】:

我正在 Unity 网站的教程上构建一个类似滚球游戏的游戏,现在我专注于游戏结束菜单,当玩家失去所有生命时,我激活游戏结束文本,是和否text 和默认情况下显示在 yes 文本后面的 rawImage ,问题是我希望 yes 文本后面的图像使用箭头键下降到 no 并上升到 yes 。我想我用按键实现了它,但我觉得我需要一个协程来构建它,因为它只是离开游戏结束功能,而不等待用户输入,所以在此之后我怎样才能以正确的模式构建它?

我现在这样做了:

public class Manager : MonoBehaviour {

private float checkPoint = 0;
public GameObject Ball;
private GameObject cam;
private Object newBall;
public int lifes;
public Text Lifetext;
private bool gameIsOver = false;
private Text gameOver;
private Text playAgain;
private Text yes;
private Text no;
private RawImage TopMenuBall;


void Awake(){
    Lifetext = GameObject.Find("Lifes").GetComponent<Text>();
    gameOver = GameObject.Find("GameOver").GetComponent<Text>();
    playAgain = GameObject.Find("PlayAgain").GetComponent<Text>();
    yes = GameObject.Find("Yes").GetComponent<Text>();
    no = GameObject.Find("No").GetComponent<Text>();
    TopMenuBall = GameObject.Find("TopMenuBall").GetComponent<RawImage>();

    gameOver.enabled = false;
    playAgain.enabled = false;
    yes.enabled = false;
    no.enabled = false;
    TopMenuBall.enabled = false;
    TopMenuBall.transform.localPosition = new Vector3(-68,-41,0);

}

void Start(){
    lifes = 3;
    Lifetext.text = " x" + lifes;
    SpawnBall ();
    cam = GameObject.Find ("Main Camera");

}

public void LifeUp(){
    lifes++;
    Lifetext.text = "X " + lifes; 
}

public void LifeDown(){
    if (lifes <= 0) {
        GameOver ();
    } else {
        lifes--;
        Lifetext.text = "X " + lifes;
    }
}

public void GameOver(){
    Debug.Log ("gameover");
    gameOver.enabled = true;
    playAgain.enabled = true;
    yes.enabled = true;
    no.enabled = true;
    TopMenuBall.enabled = true;

    if (Input.GetKeyDown (KeyCode.DownArrow)) {
        TopMenuBall.transform.localPosition = new Vector3 (-68, -82, 0);
        Debug.Log ("up");
    }
    else if (Input.GetKeyDown(KeyCode.UpArrow))
        TopMenuBall.transform.localPosition = new Vector3(-68,-41,0);
}

void SpawnBall(){
    Vector3 spawnPosition = new Vector3 (0.02f,1.4f,-39f);
    Quaternion spawnRotation = Quaternion.identity;
    newBall = Instantiate (Ball, spawnPosition, spawnRotation);
    Camera.main.GetComponent<cameraMove>().player = (GameObject)newBall;
}

当用户失去所有生命时进入游戏结束功能并出现问题,我该如何解决?

【问题讨论】:

  • 为什么是 corotine?您可以使用Vector3.Lerp 来移动图像。
  • 我想在我的马槽上做,我该怎么做?

标签: c# unity3d unity5


【解决方案1】:

您应该使用Update() 方法来获取用户输入。因为你需要不断地检查它,而Update方法正是针对这种情况而存在的,每帧调用一次。

所以,你的代码应该是这样的:

void Update() {
    if (TopMenuBall.enabled) {
        if (Input.GetKeyDown (KeyCode.DownArrow)) {
            TopMenuBall.transform.localPosition = new Vector3 (-68, -82, 0);
            Debug.Log ("up");
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow))
            TopMenuBall.transform.localPosition = new Vector3(-68,-41,0);
    }
}

【讨论】:

  • 非常感谢您的回答,这是有道理的,但只有最后一个问题,例如,我希望更新函数中的内容在游戏结束时运行,在这种情况下,如果它即使不是游戏结束也能正常工作
  • 这就是我添加“if (TopMenuBall.enabled)”命令的原因。这样,只有在启用时才会检查此输入(我看到您仅在 GameOver 功能中启用)。您也可以使用另一个变量,也许创建一个仅在正确时间设置的布尔值。
  • 朋友,我做到了,非常感谢您的帮助:)
猜你喜欢
  • 2010-11-17
  • 2015-07-05
  • 2012-04-25
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
相关资源
最近更新 更多