【问题标题】:Unity how to come back to the gameplayUnity如何回归游戏玩法
【发布时间】:2019-06-26 14:55:12
【问题描述】:

在我的游戏中,如果玩家按下 ESC,会显示一个菜单按钮,如果他点击它,他会返回主菜单。我该怎么做:

如果他在此主菜单中时再次按 ESC,则当游戏暂停时,他将返回游戏。当玩家点击按钮时,我正在这样做:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class VoltaMenu : MonoBehaviour
{
    public void BackToGame()
    {
        SceneManager.LoadScene("Main Menu");
    }
}

我该如何做另一部分?

【问题讨论】:

  • “另一部分”是什么意思?
  • 您现在正在为再次按下 ESC 后才返回游戏而苦苦挣扎。你很擅长进入主菜单场景。我说的对吗?

标签: c# unity3d


【解决方案1】:

您似乎使用了onclick function,您必须添加一个更新函数来检查玩家是否按下了退出按钮。

// Update is called once per frame
    void Update()
    {
       if (Input.GetKeyDown(KeyCode.Escape))
       {
        SceneManager.LoadScene("Main Menu");
       }
    }

在你的代码中添加这个函数,它应该会加载场景主菜单

【讨论】:

  • 这将在按下退出键时直接加载主菜单场景。但这里不是这样
【解决方案2】:

如果你想在再次按下 ESC 后返回游戏,试试这个

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.SceneManagement;
  using UnityEngine.UI;

  public class VoltaMenu : MonoBehaviour
 {

// Start is called before the first frame update
void Start()
{


}

void Update()
{ 
    if(Input.GetKeyDown(KeyCode.Escape))
    {
        if(Time.timeScale == 1)
        {
            Time.timeScale = 0;
            showPaused();
        } else if (Time.timeScale ==0){
            Time.timeScale = 1;
            hidePaused();
        }
    }
 }

// Update is called once per frame
public void BackToGame()
{
    SceneManager.LoadScene("Main Menu");
}

 void showPaused()
 { //Write code to show dailogue here
 }

 void hidePaused()
 { //Write code to hide dailogue i.e Pause menu
 } 

}

你要完成hidePaused()和showPaused()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2020-04-17
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多