如何在场景转换之间获取信息? [英] How to mantain informations between scene transition?

查看:94
本文介绍了如何在场景转换之间获取信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此工具包像游戏一样进行神庙奔跑. https://www.assetstore.unity3d.com/#/content/3292 .我想插入一个有两扇门的房间,当播放机进入房间时,它停止运行,并且用户可以用箭头键进行控制;当它通过后门离开房间时,它又开始运行.当它与门相撞时我该怎么办? 我通过替换场景来做到这一点.当玩家与我正在加载HouseScene时发生碰撞时,而当它与后门(在HouseScene中)碰撞时,我正在加载GamePlayScene时,我在GamePlayScene中实例化了一个空的游戏对象预制件.但是游戏是从头开始的.如何从我离开的地方继续游戏,并保持覆盖距离和收集硬币的轨迹?而且也适用于HouseScene.记住我在其中取得的成就.谢谢.

解决方案

您需要保存要在场景之间保留的信息. 您可能会遇到以下情况:

1)将您的信息保存在文本文件中,并在加载新场景时检索这些信息(但是这种方式有点脏",不建议使用...此列表的下一点是更好的解决方案);

2)使用 PlayerPrefs .它们提供了用于在操作系统注册表中检索/存储数据的获取器和设置器.

3)使用包含所有全局"变量(要在场景转换之间进行操作的变量)的对象(作为容器),并调用

I am working on temple run like game using this Kit. https://www.assetstore.unity3d.com/#/content/3292. I want to insert a room with two doors , when my player enters in the room it stops running and user can control with arrow keys , and when it leaves the room by back door it starts running again . what should i do when it collides with the door collides ? I am doing this by replacing the scenes. I am instantiating an empty Game-object prefab in GamePlayScene when player is colliding with that i am loading HouseScene and when it is colliding with back door (in HouseScene) i am loading GamePlayScene. but the game is starting from the beginning. how can I resume the game From where I left And keep the track of Distance covered and coins collected? And also for HouseScene . Remember the points i achieved in it. Thanks.

解决方案

You need to save the informations that you want to keep between your scenes. You have some possibilities about that:

1) Save your informations in a text file and retrieve those when you load a new scene (but this way is a bit "dirty" and not recommended...the next points of this list are better solutions);

2) Using PlayerPrefs. They provide getters and setters to retrieve/store data in/from the registry of your operative system.

3) Using an object (as a container) that contains all your "global" variables (the ones that you want to mantain between scenes' transition), and invoking the DontDestroyOnLoad function on it. That way, your "container" (with its data) will persist in the entire life cycle of the game.

这篇关于如何在场景转换之间获取信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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