How can I pass data between two levels?
来源:互联网 发布:我要开淘宝网店 编辑:程序博客网 时间:2024/05/17 00:56
问:
How do I pass information from my existing level to a new level that I'm about to load?
回答1:
The way I approach this is by taking any properties that need to be accessed from more than one scene and making them public variables in a script that is added to an empty GameObject and then I call Object.DontDestroyOnLoad on that object so it will persist through scene changes. Then it's a simple matter of using GameObject.Find to get thatGameObject
from a script in the new scene and GameObject.GetComponent to get the script. Then you can access the public variables of a persistent object from any script.
P.S. - GameObject.Find is fast but there's no need to call it every frame. To reference the persistent object script from another script, do something like this:
SomeObjectInTheNewSceneScript.js (pick a better name)
// if your persistent script is named `PersistenScript.js`private persistentScript : PersistentScript;function Start () { // Start runs once early on and makes the connection to the other script // so you don't have to do it every frame. // if your persistent game object is named `Persistent Object` var persistentGameObject : GameObject = GameObject.Find("Persistent Object"); persistentScript = persistentGameObject.GetComponent(PersistentScript);}
then you can set/get properties of persistentScript
anywhere else in SomeObjectInTheNewSceneScript
回答2:
DontDestroyOnLoad, static, PlayerPrefs to hold variables between scenes.
- How can I pass data between two levels?
- How do I pass data between Activities/Services within a single application?
- Using a delegate to pass data between two forms.
- How can I become data scientist?FAQ
- How-to: transfer hbase data between two hadoop cluster
- How can I connect two bridges (eg br0 and br1)?
- How can I access my Wubi root.disk data
- How Can I Stop
- How To Pass Arrays Between Visual Basic and C
- How to pass values between AppWidgetProvider to Activity
- Storyboards Segue Tutorial: Pass Data Between View Controllers
- Storyboards Segue Tutorial: Pass Data Between View Controllers
- How can I run two Django versions in the same server?
- How to Share Data between testcase
- Windows SDK Registry: How can I read in data from the registry?
- Windows SDK Registry: How can I write data to the registry?
- How can I create a data binding in code using WPF?
- How can I capture raw 802.11 frames, including non-data (management, beacon) frames?
- Kettle插件开发(转换步骤)
- C#文件路径
- WINDOWS下OCCI开发包下载
- A股股票小参数计算
- 《Red5 用户参考手册》之一:引言和新特性
- How can I pass data between two levels?
- base64编码类源代码(C#)
- ubuntu11.10下通过virtualbox安装winxp
- POJ-3984-迷宫问题
- Spring程序员的春天
- 类的索引
- strcpy()、memcpy()、memmove()、memset()的实现
- OCCI编程介绍
- Delphi打开外部程序或文件