Data transmission between the unity scene: Methods and considerations
Use static variables
Example:
Use Dontdestroyonload
<code>public static int score;</code>
Example:
Local data storage
<code>void Awake() { DontDestroyOnLoad(gameObject); }</code>
It is suitable for a small amount of data (for example, high scores, settings).
Example: File -based storage:
It is suitable for a large amount of data or complex data structures.Example: Use the custom data class PlayerData:
<code>void OnDisable() { PlayerPrefs.SetInt("playerScore", playerScore); }</code>
Suggestion
For simple data that does not need to be switched in the scene, use static variables.For GameObject or components that represent the lasting entity, use Dontdestroyonload.
<code>void SaveData() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite("playerData.bin"))) { writer.Write(playerData.score); // ... 保存其他数据 } }</code>
The above is the detailed content of How to Best Pass Data Between Scenes in Unity?. For more information, please follow other related articles on the PHP Chinese website!