使用参数变量Unity加载场景 [英] Load scene with param variable Unity

查看:369
本文介绍了使用参数变量Unity加载场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,有一个地图视图,其中包含50x50的瓷砖网格.当您单击图块时,您将被发送到该图块视图并攻击事物,等等.就代码而言,这些瓦片"之间的唯一区别是图块ID,也就是.网格上的哪个数字.该数字将在init传递给服务器以处理其余的信息.

In my game there is a map view that contains a 50x50 grid of tiles. When you click on the tile you get sent to that tiles view and attack things, etc. The only difference between these "tiles" as far as the code will be concerned is the tiles ID, aka. which number on the grid. That number will be passed to the server on init to handle the rest.

很明显,由于这是磁贴的唯一区别,因此创建场景"1",场景"2" ...场景"2500"并调用SceneManager.LoadScene切换到特定的磁贴视图将是一个错误.

Obviously, with that being the only difference in tiles it would be a mistake to create a scene "1", scene "2"...scene "2500" and call SceneManager.LoadScene to switch to the specific tile view.

我可以使用DontDestroyOnLoad();当单击图块以保留场景切换上的图块ID时,但1)它仅接受游戏对象,而不仅是int变量2)我不需要/不想为该变量保留该变量,而无需在图块视图中保留init.因此,尽管它可以正常工作,但似乎有点过分.

I could use DontDestroyOnLoad(); when the tile is clicked to preserve the tile ID on scene switch but 1) it only accepts gameobjects not just an int variable 2) I don't need/want to preserve that variable for anything more than the init in tile view. So while it could work it seems like overkill.

有没有一种更好的做法,实际上只是将参数传递给场景负载?

Is there a better practice for essentially just passing a parameter to a scene load?

推荐答案

您可以创建一个静态类来为您保存信息.此类不会附加到任何GameObject,并且在更改场景时不会被销毁.它是static,表示只能有一个.您不能编写StaticClassName scn = new StaticClassName()来创建新的静态类.例如,您可以直接通过StaticClassName.SomeStaticMethod()访问它们,并且可以从任何地方访问.参见以下示例,了解如何在变量中存储值,更改场景然后在该场景中使用它:

You can make a static class that holds the informaton for you. This class will not be attached to any GameObject and will not be destroyed when changing scene. It is static which means there can only be ONE of it; you can't write StaticClassName scn = new StaticClassName() to create new static classes. You access them straight through StaticClassName.SomeStaticMethod() for example and can be accessed from anywhere. See this example on how to store a value in a variable, change scene and then use it in that scene:

附加到测试"场景中游戏对象的普通Unity脚本:

A normal Unity script attached to a gameobject in Scene "Test":

using UnityEngine;
using UnityEngine.SceneManagement;
public class TestingScript : MonoBehaviour {
    void Start()
    {
        StaticClass.CrossSceneInformation = "Hello Scene2!";
        SceneManager.LoadScene("Test2");
    }
}

一个包含信息的新静态类(不继承单行为):

A new static class (not inheriting from monobehaviour) that holds information:

public static class StaticClass {
    public static string CrossSceneInformation { get; set; }
}

附加到场景"Test2"中游戏对象的脚本:

A script attached to a game object in scene "Test2":

using UnityEngine;
public class TestingScript2: MonoBehaviour {

    void Start () {
        Debug.Log(StaticClass.CrossSceneInformation);
    }
}

您不需要使整个类静态(如果出于某种原因,您需要创建它的更多实例).如果要从类(而不是变量)中删除static,则仍然可以通过StaticClass.CrossSceneInformation访问静态变量,但也可以执行StaticClass sc = new StaticClass();.使用此sc,您可以使用该类的非静态成员,但不能使用static CrossSceneInformation,因为只能有一个(因为它是静态的).

You don't need to have the entire class static (if you for some reason need to create more instances of it). If you were to remove the static from the class (not the variable) you can still access the static variable through StaticClass.CrossSceneInformation but you can also do StaticClass sc = new StaticClass();. With this sc you can use the class's non-static members but not the static CrossSceneInformation since there can only be ONE of that (because it's static).

这篇关于使用参数变量Unity加载场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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