简体   繁体   English

如何制作重新启动按钮或使用 r 重新启动场景

[英]How do I make a restart button or use r for restarting the scene

See when my player dies you can restart well that's what I wanted but I can't get it to work and I want it to where you have an option of either pressing r or the button to restart.看看我的播放器何时死亡,您可以很好地重新启动,这就是我想要的,但我无法让它工作,我希望它在您可以选择按 r 或按钮重新启动的地方。

Can I please get a couple of tips for this probably some code too but that's up to you btw I am a beginner and need much help my game is just a basic learning game and I will love help from you fellow gamedevs我可以请我得到一些提示,这可能也有一些代码,但这取决于你,顺便说一句,我是一个初学者,需要很多帮助我的游戏只是一个基本的学习游戏,我会喜欢你的游戏开发伙伴的帮助

This is my old code这是我的旧代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResetManager : MonoBehaviour
{
    public static ResetManager Instance { get; private set; }

    public event System.Action ResetEvent = new delegate {};

    public void Awake()
    {
        Instance = this;
    }

    public static void ResetScene()
    {
        Instance.ResetEvent();
    }
}

I would like to know if I could make it without scene manager我想知道我是否可以在没有场景管理器的情况下完成它

Welcome to stack overflow, here I will show a brief tutorial on how to build the key in the UI as well as execute the reset code.欢迎来到堆栈溢出,在这里我将展示如何在 UI 中构建密钥以及执行重置代码的简短教程。 To create a UI button , go to Hierarchy and right-click, you can create a Button from the UI menu as shown below.要创建UI 按钮,请转到Hierarchy并右键单击,您可以从 UI 菜单中创建一个 Button,如下所示。

在此处输入图像描述

After creating the button, attach the script to a Manager object, click the button and reference the reset command at the bottom of the event, as below, keeping in mind that the unity event does not support static method's.创建按钮后,将脚本附加到 Manager 对象,单击按钮并引用事件底部的重置命令,如下所示,请记住统一事件不支持static方法。

public void ResetScene() => SceneManager.LoadScene(SceneManager.GetActiveScene().name);

在此处输入图像描述

After performing the above steps, your button will restart the scene when clicked.执行上述步骤后,您的按钮将在单击时重新启动场景。


You can also restart the scene when you press the R key by adding the following code.您也可以通过添加以下代码在按下R键时重新启动场景。

private void Update()
{
    if (Input.GetKeyDown(KeyCode.R)) ResetScene();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM