State class :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class SaveState
{
public float posX;
public float posY;
public float posZ;
public float rotX;
public float rotY;
public float rotZ;
public float rotW;
public float scaX;
public float scaY;
public float scaZ;
public DateTime LastSaveTime { set; get; }
}
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);
SaveState data = new SaveState();
formatter.Serialize(stream, data);
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;
}
}
}
Test script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveTest : MonoBehaviour
{
private void Update()
{
if(Input.GetKeyDown(KeyCode.T))
{
var player = GameObject.Find("Player");
SaveState saveState = new SaveState();
saveState.posX = player.transform.position.x;
saveState.posY = player.transform.position.y;
saveState.posZ = player.transform.position.z;
saveState.rotX = player.transform.rotation.x;
saveState.rotY = player.transform.rotation.y;
saveState.rotZ = player.transform.rotation.z;
saveState.rotW = player.transform.rotation.w;
saveState.scaX = player.transform.localScale.x;
saveState.scaY = player.transform.localScale.y;
saveState.scaZ = player.transform.localScale.z;
//Save data from PlayerInfo to a file named players
SaveManager.Save(saveState);
}
if(Input.GetKeyDown(KeyCode.L))
{
//Display loaded Data
var player = GameObject.Find("Player");
player.transform.position = new Vector3(SaveManager.Load().posX, SaveManager.Load().posY, SaveManager.Load().posZ);
player.transform.rotation = new Quaternion(SaveManager.Load().rotX, SaveManager.Load().rotY, SaveManager.Load().rotZ, SaveManager.Load().rotW);
player.transform.localScale = new Vector3(SaveManager.Load().scaX, SaveManager.Load().scaY, SaveManager.Load().scaZ);
}
}
}
I can see the created saved file player.bin 1 KB size.
This is a screenshot when I press the T button to save : On the left bottom game window. And the inspector player info position and rotation when saving.
I press the T button once to save while the player is in coroutine. When the game start the player is not moving but rotating using coroutine. Not sure if the coroutine if saving while the player is rotating make the problem.
This is a screenshot when pressing the L button and the inspector of the player position and rotation :
Not same position and rotation when loading.