- Home /
Save game for Steam
I make the game on Unity3D and I have a save system there. When I upload assembly of my game to the Steam, then it works fine, but when I upload an update to Steam, then then all the saving from the old assembly is overwritten by the new one. How can I prevent this?
Here how I save my game:
public void ButtonSave()
{
PlayerPrefs.SetString("date time" + currentActiveSlot, System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + " ");
PlayerPrefs.SetFloat("transform position x" + currentActiveSlot, playerTransform.position.x);
PlayerPrefs.SetFloat("transform position y" + currentActiveSlot, playerTransform.position.y);
PlayerPrefs.SetFloat("transform position z" + currentActiveSlot, playerTransform.position.z);
PlayerPrefs.SetInt("latestSaveSlot", latestSaveSlot);
PlayerPrefs.SetInt("act number" + currentActiveSlot, gameManagerScript.currentActNumber);
PlayerPrefs.SetString("sceneName" + currentActiveSlot, SceneManager.GetActiveScene().name);
PlayerPrefs.SetInt("slotImage" + currentActiveSlot, imageNumberForCurrentSlot);
PlayerPrefs.Save();
}
Edit 1 Here is my new class, where I save all my data:
[Serializable]
public class Saves : MonoBehaviour
{
public string dateTime;
public float transformPositionX;
public float transformPositionY;
public float transformPositionZ;
public int latestSaveSlot;
public int actNumber;
public string sceneName;
public int slotImage;
public int currentActiveSlot;
public int stepNumber;
}
and here how I save it:
public void ButtonSave()
{
saves.dateTime = System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
saves.transformPositionX = playerTransform.position.x;
...
}
But in my previous code I've loaded data like this from the saveslot I need: gameManagerScript.currentActNumber = PlayerPrefs.GetInt("act number" + currentActiveSlot);
, and now I can't do it like this: gameManagerScript.currentActNumber = saves.actNumber + currentActiveSlot;
, because it change value of the variable. How can I load data from the saveslot I need?
Edit 2 Here how I save the data:
public void ButtonSave()
{
...
var serializedSave = JsonUtility.ToJson(saves);
var saveFileName = Application.persistentDataPath + "/Save_" + currentActiveSlot + ".save";
File.WriteAllText(saveFileName, serializedSave);
}
Answer by x4637x · Oct 30, 2017 at 09:13 AM
PlayerPrefs is not a permanent saving solution. You should create your own saving file and place them under Application.persistentDataPath
. For the file format, you could use anything you want. Json, XML or even text file will do. It is completely up to you.
First of all, you should use ScriptableObject ins$$anonymous$$d of $$anonymous$$onoBehaviour for your serializable class. Secondly, what you have right now is still an object instance that lives in your computer's memory. If you want to save it for each game session, you need to save it to your hard disk. I suggest you use AssetDatabase.CreateAsset
to save your ScriptableObject instance to an asset file at the Application.persistentDataPath
. And for loading, you could use AssetDatabase.LoadAssetAtPath
. PS: If you do not want to edit your save file directly you could use EditorUtility.CopySerialized
after loaded your save asset file for copying your saved data to the current data instance.
Forgot to check one important thing with you, when will you call this saving/loading function? At play-time or edit-time? If you are doing this save/load at play-time, AssetDatabase & ScriptableObject is no good for you, since it needs to use UnityEditor class. You can try JsonUtility
or if you are good with the database, you could try using a SQLite database file for all your saves.
Sorry, I forgot to add some code. Yes, I use JsonUtility, now I will update my question. I don't understand, how to load data from the current slot
Answer by Propagant · Oct 30, 2017 at 12:17 PM
Hey, you should check out this asset.. https://www.assetstore.unity3d.com/en/#!/content/53978
Which is the asset for saving and loading whole scene... But if you are interested how it works [basically] you can check out the Save-It free version here
https://mega.nz/#!W4QFVIbY!E-nA92Ylevd5o-PYJIZGUhJdAuSDjJf2j0oZ0KVdmm0
Your answer
![](https://koobas.hobune.stream/wayback/20220612142143im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Editor Crashes When Switching from VR to Standalone 0 Answers
A real head-scratcher 0 Answers