Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/baba-s/UniSceneDataTransfer
Unity package to easily pass data at scene transition.
https://github.com/baba-s/UniSceneDataTransfer
kogane-unity-lib unity unity-package unity-package-manager unity-scripts unity3d
Last synced: about 2 months ago
JSON representation
Unity package to easily pass data at scene transition.
- Host: GitHub
- URL: https://github.com/baba-s/UniSceneDataTransfer
- Owner: baba-s
- License: mit
- Created: 2020-01-19T04:03:18.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-19T09:17:14.000Z (over 4 years ago)
- Last Synced: 2024-10-30T00:04:19.829Z (3 months ago)
- Topics: kogane-unity-lib, unity, unity-package, unity-package-manager, unity-scripts, unity3d
- Language: C#
- Homepage:
- Size: 21.5 KB
- Stars: 11
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-unity-open-source-on-github - Unity Scene Data Transfer - Easily pass data at scene transition (Script Utility)
README
[日本語の Readme はこちら](https://github.com/baba-s/unity-scene-data-transfer/blob/master/README_JP.md)
# Uni Scene Data Transfer
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/baba_s/20200119/20200119140300.png)
Unity package to easily pass data at scene transition.
## Usages
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/baba_s/20200119/20200119135440.png)
The name of the scene and the name of the component that controls the scene must be the same.
```cs
public class ResultData
{
public int Score;
public int Rank;
}
```Define the data class to be passed to the scene.
```cs
using UnityEngine;public class ResultScene : MonoBehaviour
{
```MonoBehaviour to control the scene,
```cs
using KoganeUnityLib;
using UnityEngine;public class ResultScene : SimpleSceneBase
{
```Change to inheritance SimpleSceneBase.
- Add `using KoganeUnityLib;` .
```cs
using KoganeUnityLib;
using UnityEngine;public class ResultScene : SimpleSceneBase
{
private void Start()
{
Debug.Log( entryData.Score );
Debug.Log( entryData.Rank );
}
```Then, the passed data can be referenced by entryData property.
```cs
var data = new ResultData
{
Score = 5000,
Rank = 3,
};
ResultScene.Load( data );
```After that, by writing the above code in another scene, you can transition the scene while passing data.
## Remarks: Awake, OnEnable
```cs
public class ResultScene : SimpleSceneBase
{
// Cannot
private void Awake()
{
Debug.Log( entryData.Score );
Debug.Log( entryData.Rank );
}
// Cannot
private void OnEnable()
{
Debug.Log( entryData.Score );
Debug.Log( entryData.Rank );
}
```- entryData property is not available for Awake, OnEnable.
## Remarks: Launch directly
When launching the scene directly, entryData is null.
```cs
public class ResultScene : SimpleSceneBase
{
private void Start()
{
var data = entryData ?? new ResultData();Debug.Log( data.Score );
Debug.Log( data.Rank );
}
```Therefore, by writing the above code,
When the scene is started directly, the dummy data can be used.```cs
using System;[Serializable]
public class ResultData
{
public int Score;
public int Rank;
}
```Alternatively, by applying the Serializable attribute to the data class,
![](https://cdn-ak.f.st-hatena.com/images/fotolife/b/baba_s/20200119/20200119135443.png)
entryData used when launching the scene directly can be set in Unity Inspector.