1

I have 3 scenes. Gameplay, Main Menu, Manager. On the Manager I have a Save Manager object with a Save Manager script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.IO;

public class SaveManager : MonoBehaviour
{
    public static void Save(SaveState player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/player.bin";
        FileStream stream = new FileStream(path, FileMode.Create);

        formatter.Serialize(stream, player);
        stream.Close();
    }

    public static SaveState Load()
    {
        string path = Application.persistentDataPath + "/player.bin";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            SaveState data = formatter.Deserialize(stream) as SaveState;
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }
}

And a class : This Save State class should be handle later the state of everything in the game.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class SaveState
{
    [Serializable]
    public struct SerializableVector3
    {
        public float X;
        public float Y;
        public float Z;

        public SerializableVector3(Vector3 v)
        {
            X = v.x;
            Y = v.y;
            Z = v.z;
        }

        // And now some magic
        public static implicit operator SerializableVector3(Vector3 v)
        {
            return new SerializableVector3(v);
        }

        public static implicit operator Vector3(SerializableVector3 sv)
        {
            return new Vector3(sv.X, sv.Y, sv.Z);
        }
    }

    [Serializable]
    public struct SerializableQuaternion
    {
        public float X;
        public float Y;
        public float Z;
        public float W;

        public SerializableQuaternion(Quaternion q)
        {
            X = q.x;
            Y = q.y;
            Z = q.z;
            W = q.w;
        }

        public static implicit operator SerializableQuaternion(Quaternion q)
        {
            return new SerializableQuaternion(q);
        }

        public static implicit operator Quaternion(SerializableQuaternion sq)
        {
            return new Quaternion(sq.X, sq.Y, sq.Z, sq.W);
        }
    }

    public SerializableVector3 position;
    public SerializableQuaternion rotation;
    public SerializableVector3 scale;

    public SaveState(Vector3 pos, Quaternion rot, Vector3 sca)
    {
        position = pos;
        rotation = rot;
        scale = sca;
    }

    public void ApplyToPlayer(Transform player)
    {
        player.localPosition = position;
        player.localRotation = rotation;
        player.localScale = scale;
    }
}

And the script that is attached to the Player in the Game scene :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerState : MonoBehaviour
{
    public void Save()
    {
        SaveState saveState = new SaveState(transform.localPosition,
            transform.localRotation, transform.localScale);
       SaveManager.Save(saveState);
    }

    public void Load()
    {
        var playerInfo = SaveManager.Load();
        playerInfo.ApplyToPlayer(transform);
    }
}

Before that the PlayerState was with the same scene as the SaveManager and I used in Update with Input keys to save and load and it worked fine.

Now I want instead using Update and input keys to use the Main Menu scene ui buttons Save and Load I have. The problem is that the Player is one one scene the Main Menu is another scene and when the player start a new game the scene where the Player is unload and load again so it will lost reference in the main menu ui buttons.

Hierarchy

The Save/Load ui buttons in the Main Menu under Save System this are the ui buttons I want to call on the On Click evenets for the Save/Load functions from the PlayerState script.

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120

1 Answers1

2

I think your best bet would be to just make sure your manager and your UI is persistent, so simply set:

private void Awake()
{
    DontDestroyOnLoad(this);
}

And then in their updates just have a switch statement checking for the current scene so you'll only update what is necessary for that scene like so:

private void Update()
{
    switch(SceneManager.GetActiveScene().name)
    {
        case "Game":
            //Do you game update stuff...
            break;

        case "Menu":
            //Do your menu update stuff...
            break;
    }
}

I hope that helps :)