Save a game scene to file at runtime with many dynamically instanced gameObjects in and textures

I’ve created an RPG with lots of dynamically instanced character game objects in, each with randomly generated names and textures, they even have dynamically generated sprite icons rendered from a camera as texture2d and saved as sprites in their root game object. Each character has a lot of stats and clothes/weapon/items each with their own meshes reference on the character.

Now I need to save the scene. I’ve built a save/load UI but after looking at many serialization tutorials none appear simple. The stock serialization to file unity tutorial doesn’t even talk about serializing unity types and instead just talks about simple types.

I have put all my character and item references into a single script container… how do I save the scene and reload it at runtime with all my characters , stats, items, textures and positions in place.

I hope it would be something simple like Application.saveScene(filename)… but alas no.

Hi. I need same save scene solution. Anyone knows something good?

it is alot easier than what it seems, bear with me it is a bit long, I hope this helps.

first step:

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

next declare all your variables (I have mine as public, as this is the core for my character):

DateTime    now         = DateTime.Now;
string      format      = "dd MM yyyy  hh:mm";
string      loadDate;
string      saveDate;

public int  Level        = 1;
public int  currentXP    = 0;
public int  toNxtLvl     = 500;
public int  health       = 5;
public int  strength     = 1;
public int  stanima      = 1;
public int  agility      = 1;
Vector3     playerPos;

public static _GameControl control; //with this you can acces the variables in this class from another //script by going _GameControl.control.health;

now you need to put in this to ensure that the data in this script is stored between scenes and so you cant accidently spawn 2 of this class ( so if you change scene the original will change scene with you and will remove any copys in the new scene) to do that you need this:

void Awake()
{
    if (control == null)
    {
        DontDestroyOnLoad(gameObject);
        control = this;
    }
    else if(control != null)
    {
        Destroy(gameObject);
    }
}

now you need a new class that can serialise your data which looks like this:

[System.Serializable]
class playerData

{

public int      level;
public int      curXp;
public int      toNxt;
public int      health;
public int      strength;
public int      stanima;
public int      agility;
public Vector3  pPos;

//these all need to be public so you can access them through save and load
}

now you need a Save Function:

void Save()

{
    playerPos = GameObject.Find("Player").transform.position;

    saveDate = (now.ToString(format));

    BinaryFormatter binary  = new BinaryFormatter();
    FileStream file         = File.Create(Application.persistentDataPath +"/SaveGame.dat"); //the creates a file in the unity app data path
    playerData dat          = new playerData();

    dat.level       = Level;    //this transfers the data you want to your serializable class
    dat.curXp       = currentXP;                      
    dat.toNxt       = toNxtLvl;                           
    dat.health      = health;                           
    dat.strength    = strength;
    dat.stanima     = stanima;
    dat.agility     = agility;
    dat.pPos        = playerPos;

    binary.Serialize(file, dat);
    file.Close();

and finally the load function which is pretty much the same as the save but kinda reversed:

void Load()
{
    if (File.Exists(Application.persistentDataPath + "/SaveGame.dat"))
    {
        BinaryFormatter binary  = new BinaryFormatter();
        FileStream file         = File.Open(Application.persistentDataPath + "/SaveGame.dat", FileMode.Open);
        playerData dat          = (playerData)binary.Deserialize(file);
        file.Close();

        Level       = dat.level; //this sets all the saved data back onto your character or npc's
        currentXP   = dat.curXp;
        toNxtLvl    = dat.toNxt;
        health      = dat.health;
        strength    = dat.strength;
        stanima     = dat.stanima;
        agility     = dat.agility;
        playerPos   = dat.pPos;
    }
}