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: 4 months ago
JSON representation

Unity package to easily pass data at scene transition.

Lists

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.