baba-s / UniSceneDataTransfer

Unity package to easily pass data at scene transition.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

日本語の Readme はこちら

Uni Scene Data Transfer

Unity package to easily pass data at scene transition.

Usages

The name of the scene and the name of the component that controls the scene must be the same.

public class ResultData
{
    public int Score;
    public int Rank;
}

Define the data class to be passed to the scene.

using UnityEngine;

public class ResultScene : MonoBehaviour
{

MonoBehaviour to control the scene,

using KoganeUnityLib;
using UnityEngine;

public class ResultScene : SimpleSceneBase<ResultScene, ResultData>
{

Change to inheritance SimpleSceneBase.

  • Add using KoganeUnityLib; .
using KoganeUnityLib;
using UnityEngine;

public class ResultScene : SimpleSceneBase<ResultScene, ResultData>
{
    private void Start()
    {
        Debug.Log( entryData.Score );
        Debug.Log( entryData.Rank );
    }

Then, the passed data can be referenced by entryData property.

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

public class ResultScene : SimpleSceneBase<ResultScene, ResultData>
{
    // 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.

public class ResultScene : SimpleSceneBase<ResultScene, ResultData>
{
    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.

using System;

[Serializable]
public class ResultData
{
    public int Score;
    public int Rank;
}

Alternatively, by applying the Serializable attribute to the data class,

entryData used when launching the scene directly can be set in Unity Inspector.

About

Unity package to easily pass data at scene transition.

License:MIT License


Languages

Language:C# 100.0%