Hello
I have a few problems figuring out how to load the data back after saving using Binary Formater, here is my code:
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
//You must include these namespaces
//to use BinaryFormatter
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveSystem : MonoBehaviour {
[System.Serializable]
public class SaveEntry{
public int _CurHealth;
public int _MaxHealth;
public int _CurExp;
public int _MaxExp;
public int _Damage;
public int _PhiDefense;
public int _MagDefense;
public int _Gold;
public float _PlayerPositionX;
public float _PlayerPositionY;
public float _PlayerPositionZ;
public string _Level;
}
//High score table
SaveEntry SaveData = new SaveEntry();
public void Save(string SaveSlot){
GameObject _Player = GameObject.FindGameObjectWithTag("Player");
playerHealth _Healthscript = _Player.GetComponent<playerHealth>();
attack _AttackScript = _Player.GetComponent<attack>();
SaveData._CurHealth = _Healthscript.CurHealth;
SaveData._MaxHealth = _Healthscript.MaxHealth;
SaveData._CurExp = _Healthscript.CurExp;
SaveData._MaxExp = _Healthscript.MaxExp;
SaveData._Damage = _AttackScript.PlayerDamage;
SaveData._PhiDefense = _Healthscript.PhisicalDefense;
SaveData._MagDefense = _Healthscript.MagicalDefense;
SaveData._Gold = _Healthscript.Gold;
SaveData._PlayerPositionX = _Player.transform.position.x;
SaveData._PlayerPositionY = _Player.transform.position.y;
SaveData._PlayerPositionZ = _Player.transform.position.z;
SaveData._Level = Application.loadedLevelName;
//Get a binary formatter
var b = new BinaryFormatter();
//Create an in memory stream
var m = new MemoryStream();
//Save the scores
b.Serialize(m, SaveData);
//Add it to player prefs
PlayerPrefs.SetString(SaveSlot, Convert.ToBase64String(m.GetBuffer()));
PlayerPrefs.Save();
Debug.Log ("Save complete " + SaveSlot);
}
public void Load(string SaveSlot){
GameObject _Player = GameObject.FindGameObjectWithTag("Player");
playerHealth _Healthscript = _Player.GetComponent<playerHealth>();
attack _AttackScript = _Player.GetComponent<attack>();
//Get the data
var data = PlayerPrefs.GetString(SaveSlot);
//If not blank then load it
if(!string.IsNullOrEmpty(data))
{
//Binary formatter for loading back
var b = new BinaryFormatter();
//Create a memory stream with the data
var m = new MemoryStream(Convert.FromBase64String(data));
SaveData = b.Deserialize(m);
_Healthscript.CurHealth = SaveData._CurHealth;
_Healthscript.MaxHealth = SaveData._MaxHealth;
_Healthscript.CurExp = SaveData._CurExp;
_Healthscript.MaxExp = SaveData._MaxExp;
_AttackScript.PlayerDamage = SaveData._Damage;
_Healthscript.PhisicalDefense = SaveData._PhiDefense;
_Healthscript.MagicalDefense = SaveData._MagDefense;
_Healthscript.Gold = SaveData._Gold;
_Player.transform.position = new Vector3 (SaveData._PlayerPositionX,SaveData._PlayerPositionY,SaveData._PlayerPositionZ);
//Application.LoadLevel(SaveData._Level);
}
}
}
My problem is I dont know how to properly save the data back into the SaveData variable after loading the playerpref and converting it with the BinaryFormater, another problem I have is I dont know how to load the level the player was in when he saved because If do application.loadlevel() all the data I just loaded obviously gets erased.