简体   繁体   English

Unity3D初学者RestartButton。 在场景之间共享信息?

[英]Unity3D beginner RestartButton. Sharing information between scenes?

I want a restart menu that works for all my levels, as I load level 1 there is this script setting an integer to 1 on an empty game object. 我想要一个适用于我所有级别的重启菜单,当我加载级别1时,此脚本在一个空的游戏对象上将整数设置为1。

using UnityEngine;
using System.Collections;

public class SetRestart : MonoBehaviour {

    public int Setrestart = 1;

    void awake ()
    {
        DontDestroyOnLoad (this);
    }
   }

When you fail the level you get to the next scene called: LostMenu. 如果您未通过该级别,则转到下一个场景:LostMenu。 You have the option to restart that level you were playing or quit. 您可以选择重新启动您正在玩或退出的关卡。 So here I made a button to restart and attached this script to it: 所以在这里,我做了一个重新启动的按钮,并将此脚本附加到了它:

using UnityEngine;
using System.Collections;

public class RestartButton : MonoBehaviour 
{
    public int Setrestart;

    void Start()
    {
        if (Setrestart == 1) {
            Application.LoadLevel("Main");
        }
    }
 }

(Plan is to make 50 levels and also 50 if statements, this is just the first one for the first level called "Main", every level will have its own number). (计划要制作50个级别,如果要声明50个,这只是第一个级别的“ Main”的第一个,每个级别都有其自己的编号)。 The problem is that nothing happens when I click on the button (Screenshot: http://prntscr.com/9tf4dd ) and that when I load the LostMenu screen nothing happens to the int and it stays at 0 in the inspector while in the level scene called: "Main" I said to it to give it 'int = 1.' 问题是,当我单击按钮(屏幕截图: http ://prntscr.com/9tf4dd)时,什么也没有发生,并且当我加载LostMenu屏幕时,int却没有任何反应,并且在关卡中,它保持在0场景称为:“主”我对它说是“ int = 1”。 The number 1 stays in the "Main" scene and doesn't go to the menu scene. 数字1停留在“主”场景中,而不进入菜单场景。

Level 1 is scene: "Main". 级别1是场景:“主要”。

The menu to restart when you lose is scene: "LostMenu". 丢失时要重新启动的菜单是场景:“ LostMenu”。

Am I clear? 我清楚吗? Sorry for my bad English and thank you in advance. 对不起,我的英语不好,谢谢你。

I use a static 'Globals' class to persist data between scenes in Unity. 我使用静态的“ Globals”类在Unity中的场景之间保留数据。

You could use something like this to store the name of the last level that you played. 您可以使用类似的方法来存储您上次玩的关卡的名称。

public static class Globals
{
    public static string LastLevel {get; set;}
}

Then you could just write to this string at the start of your level and read it in your restart button handler. 然后,您可以在级别开始时写入此字符串,然后在重新启动按钮处理程序中读取它。

In order for RestartButton to know about the Setrestart value stored in SetRestart you need to have a reference to the GO with the SetRestart component. 为了使RestartButton知道存储在SetRestartSetrestart值,您需要使用SetRestart组件对GO进行引用 Right now RestartButton is checking its own Setrestart value, which defaults to 0. This is why it doesn't work. 现在, RestartButton正在检查自己的 Setrestart值,该值默认为0。这就是为什么它不起作用的原因。

Instead you need this: 相反,您需要这样做:

public class RestartButton : MonoBehaviour 
{
    GameObject persistentObject;

    void Start()
    {
        persistentObject = GameObject.Find("NameOfGameObject");
        int shouldReset = (persistentObject.GetComponent<SetRestart>() as SetRestart).Setrestart;
        if (shouldReset == 1) {
            Application.LoadLevel("Main");
        }
    }
 }

GameObject.Find isn't great, performance wise, but for a once-off lookup, it's fine. GameObject.Find并不是很好的性能,但是对于一次查找,就可以了。 There are also other ways of getting the reference, but in the case of objects that persist across scene changes, this is the only way (note that I'm saving the GameObject reference to a field, while not neccessary here it is good practice so you aren't GO.Find()ing all the time). 还有其他获取引用的方法,但是如果对象在场景变化中仍然存在,则这是唯一的方法(请注意,我将GameObject引用保存到了一个字段,虽然在此处不是必需的但这是一种很好的做法,因此您并非一直都在GO.Find()ing)。

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

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