简体   繁体   English

如何在 Unity 中的 2 个场景之间进行通信?

[英]How to communicate between 2 scenes in Unity?

I would like to make a color system for my player.我想为我的播放器制作一个颜色系统。 In my game scene the player can pick up the coins and the amount of coins will be save with playerprefs, but I don' know how can I use the amount of coins in my menu scene.在我的游戏场景中,玩家可以捡起硬币,硬币数量将通过 playerprefs 保存,但我不知道如何在菜单场景中使用硬币数量。 And I need some help to a player color selecter too.我也需要一些玩家颜色选择器的帮助。 When the player select a color than in my game scene must instiate the player with the color.当玩家 select 一个颜色比在我的游戏场景中必须提示玩家用颜色。 Soo I think, i need to know how to communicate between 2 scenes. Soo 我想,我需要知道如何在两个场景之间进行交流。 Can somebody help me with some tutorial?有人可以帮我做一些教程吗?

There are multiple ways to achieve this, which I feel comes down to a matter of taste: save data to file, use DoNotDestroyOnLoad...有多种方法可以实现这一点,我觉得这归结为一个品味问题:将数据保存到文件,使用 DoNotDestroyOnLoad ...

But from what I understand, the recommended way now is to create a "manager scene" which will stay alive throughout the lifetime of your app and pass data to and from your other scenes as they are opened and closed, instead of using DontDestroyOnLoad:但据我了解,现在推荐的方法是创建一个“管理场景”,它将在您的应用程序的整个生命周期内保持活跃,并在打开和关闭其他场景时将数据传入和传出,而不是使用 DontDestroyOnLoad:

It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads.建议避免使用 DontDestroyOnLoad 来持久化您希望在场景加载中存活的管理器游戏对象。 Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress.相反,创建一个包含所有管理器的管理器场景,并使用 SceneManager.LoadScene(, LoadSceneMode.Additive) 和 SceneManager.UnloadScene 来管理您的游戏进度。 src源代码

See the Unity guide here .请参阅此处的 Unity 指南。 Basically you would have 2 scenes open at the same time, at any given moment: the manager scene and whatever the actual active game scene is.基本上,您将在任何给定时刻同时打开 2 个场景:经理场景和实际活动的游戏场景。 Then you can communicate between scripts in the two open scenes via event delegates.然后您可以通过事件委托在两个打开的场景中的脚本之间进行通信。 The way it would work is:它的工作方式是:

  1. Player selects color in scene1玩家在场景1中选择颜色
  2. Color is sent from scene1 to manager scene via event delegate颜色通过事件委托从场景 1 发送到经理场景
  3. scene1 is unloaded and scene2 is loaded卸载scene1,加载scene2
  4. Color is sent from manager scene to scene2颜色从经理场景发送到场景 2

This is the approach I've been using for a project now which looks like this:这是我现在用于项目的方法,如下所示: 在此处输入图像描述

"0-Attract", "1-Sealant", "2-Paint", and "3-Conclusion" are my actual game scenes, and "Manager Scene" contains everything that exists in every other scene (thus no reason to kill and respawn them) as well as all of my "manager" scripts which handle the passing of data between scenes. “0-Attract”、“1-Sealant”、“2-Paint”和“3-Conclusion”是我实际的游戏场景,而“Manager Scene”包含了其他场景中存在的所有内容(因此没有理由杀死和重新生成它们)以及我所有处理场景之间数据传递的“管理器”脚本。

Note that multi-scene editing can be confusing at first, as there are new things you need to pay attention to (ie, which scene is currently "active" and what that means) so be sure to go through the unity guide I posed above.请注意,多场景编辑起初可能会令人困惑,因为您需要注意一些新事物(即,当前哪个场景“活动”以及这意味着什么),因此请务必通过我上面提出的统一指南 go . Good luck!祝你好运!

you can Call DontDestroyOnLoad method on any object that you want that will remain wen you are moving the a new scene so it will not be destroyed when scene is closing.您可以在您想要的任何 object 上调用 DontDestroyOnLoad 方法,当您移动新场景时,它不会被破坏。

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

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