Here is a simple saving script I wrote for my game. Feel free to use it or to critique it. I didn’t add any serialization yet so it isn’t super fancy. But it could be useful for someone who wants to simple Save and Load objects/other data.
Code
//used for saving and loading data
//call Set() to set something, Save() to save these, and Load() to load them back up.
public bool shouldAutoSave; //should this autoSave?
public float autoSaveTime = 10; //how often the autoSave should happen
public string saveBase = "i"; //what the base of all saves are, this is useful if you are already using another save system
public bool sendWarnings = true; //sends warnings
//below is the static version of saveBase, you cannot edit this after game starts
private static string saveBaseD; //what the base of all saves are, this is useful if you are already using another save system
void Awake ()
{
//sets the public variables to statics
saveBaseD = saveBase;
}
public static void Set(string idName, float value) //sets "value" with the name "idName"
{
PlayerPrefs.SetFloat(saveBaseD + idName, value);
}
public static void Set(string idName, int value) //sets "value" with the name "idName"
{
PlayerPrefs.SetInt(saveBaseD + idName, value);
}
public static int LoadInt(string idName) //loads int
{
return PlayerPrefs.GetInt(saveBaseD + idName);
}
public static string LoadString(string idName) //loads string
{
return PlayerPrefs.GetString(saveBaseD + idName);
}
public static float LoadFloat(string idName) //loads float
{
return PlayerPrefs.GetFloat(saveBaseD + idName);
}
public static void SetPosition (string idName, Transform whatToSet)
{
PlayerPrefs.SetFloat(saveBaseD + idName + "posX", whatToSet.position.x);
PlayerPrefs.SetFloat(saveBaseD + idName + "posY", whatToSet.position.y);
PlayerPrefs.SetFloat(saveBaseD + idName + "posZ", whatToSet.position.z);
} //sets the position of "whatToSet" with the name "idName"
public static void SetRotation (string idName, Transform whatToSet)
{
PlayerPrefs.SetFloat(saveBaseD + idName + "rotX", whatToSet.rotation.x);
PlayerPrefs.SetFloat(saveBaseD + idName + "rotY", whatToSet.rotation.y);
PlayerPrefs.SetFloat(saveBaseD + idName + "rotZ", whatToSet.rotation.z);
PlayerPrefs.SetFloat(saveBaseD + idName + "rotW", whatToSet.rotation.w);
} //sets the rotation of "whatToSet" with the name "idName"
public static void SetPositionAndRotation (string idName, Transform whatToSet)
{
SetPosition(idName, whatToSet);
SetRotation(idName, whatToSet);
} //sets the position and rotation of "whatToSet" with the name "idName"
public static Vector3 LoadPosition (string idName)
{
return(new Vector3(PlayerPrefs.GetFloat(saveBaseD + idName + "posX"), PlayerPrefs.GetFloat(saveBaseD + idName + "posY"), PlayerPrefs.GetFloat(saveBaseD + idName + "posZ")));
} //loads the position of "idName" and sets it to the transform "loadToWhat"
public static Quaternion LoadRotation (string idName)
{
return(new Quaternion(PlayerPrefs.GetFloat(saveBaseD + idName + "rotX"), PlayerPrefs.GetFloat(saveBaseD + idName + "rotY"), PlayerPrefs.GetFloat(saveBaseD + idName + "rotZ"), PlayerPrefs.GetFloat(saveBaseD + idName + "rotZ")));
} //loads the rotation of "idName" and sets it to the transform "loadToWhat"
void Start ()
{
if (shouldAutoSave && autoSaveTime < 5 && sendWarnings) //sends warning about auto saving alot
{
Debug.LogWarning("Are you sure you want to auto save every " + autoSaveTime + " seconds? *You can disable this notice by turning sendWarnings off in the DataManager.");
}
StartCoroutine("AutoSave");
}
IEnumerator AutoSave ()
{
while (shouldAutoSave)
{
SaveData();
yield return new WaitForSeconds(autoSaveTime);
}
}
public static void SaveData () //saves all data
{
PlayerPrefs.Save();
}
void OnApplicationQuit() //saves data when you quit the game
{
SaveData();
}