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;
}
}