在菜单中按回后如何恢复游戏 [英] How to resume game after pressing back in menu

查看:31
本文介绍了在菜单中按回后如何恢复游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试在游戏中制作一个暂停菜单.当按逃脱键时,暂停游戏进入菜单,但是现在我希望能够在菜单中按回去并继续游戏.到目前为止,我只能暂停游戏,不能后退.另外,如果我按菜单中的播放",它将从我的教程场景而不是当前场景开始.有什么聪明的方法可以做到这一点吗?无需重置我的游戏.

Hey I am trying to make a pause menu in my game. When escape is pressed pause game go to menu, but now I want to be able to press back in menu and resume my game. So far I can only pause game and cant press back. Also if i press Play in menu it starts at my tutorial scene and not the current scene. Is there a smart way to do this? Without resetting my game.

`using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MenuScript : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            if (Time.timeScale == 0)
            {
                Time.timeScale = 1;
            }
            else
            {
                Time.timeScale = 0;
            }
            SceneManager.LoadScene("Menu");




        }

    }
}`

推荐答案

我知道这并不是一个简单的问题,因为您似乎对Unity陌生.

I get that this isn't straightforward problem since you seem new to Unity.

您的想法正确,更改时间范围将冻结现场的所有特工.但是,如果您加载一个新场景,则需要重新加载游戏场景-丢失您拥有的所有数据(那不是您想要的).我的建议是在游戏场景上创建一个叠加元素(UI),然后仅显示/隐藏它.在线有多个教程将此用作初始点.让我知道您是否需要更多帮助.

You got the idea correctly, changing the time scale will freeze all agents on the scene. HOWEVER, if you load a new scene you'll need to reload the game scene - losing any data you had (and that is not what you want). My advice is creating an overlay element (UI) on the game scene and just show/hide it. There are multiple tutorials online use this as an starting point. Let me know if you require more help.

代码示例

// Update is called once per frame
void Update()
{
  if (Input.GetKey(KeyCode.Escape))
  {
    if (Time.timeScale == 0)
    {
        Time.timeScale = 1;
        pauseMenu.gameObject.setActive(true);
    }
    else
    {
        Time.timeScale = 0;
        pauseMenu.gameObject.setActive(false);
     }
  }
}

您将需要使用Unity编辑器对该脚本上附加的pauseMenu游戏对象的引用.

You will need a reference to the pauseMenu game object attached on this script using the Unity editor.

这篇关于在菜单中按回后如何恢复游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆