Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leandroreschke · May 27, 2014 at 06:55 AM · androidserializationdeletesaveloadserialize

Problem to add a delete method to a saveload script C#

I have this SaveLoad code and i'm trying to make a delete funtion, tried to use SaveLoad.SavedGames.remove = game.current; but not worked, can someone help me? The script just create a file with player names, i know how to delete the file, but i want to delete just one player, by this i mean, just one string from the saved file. here is my code: using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using System.IO;

 public static class SaveLoad {
 
     public static List<Game> savedGames = new List<Game>();
             
     //it's static so we can call it from anywhere
     public static void Save() {
         SaveLoad.savedGames.Add(Game.current);
         BinaryFormatter bf = new BinaryFormatter();
         //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
         FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
         bf.Serialize(file, SaveLoad.savedGames);
         file.Close();
     }    
     
     public static void Load() {
         if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
             Debug.Log(Application.persistentDataPath);
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
             SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
             file.Close();
         }
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slyrfecso1 · Sep 02, 2015 at 09:33 AM 0
Share

Hi,

Thanks for sharing your SaveLoad method. Have you any idea how can I the savedGames.gd to www?

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by leandroreschke · May 27, 2014 at 09:16 PM

Already solved my problem ^_^, i openwrite the file, delete just the game.current(current Player) and save it again. The problem with playerprefs is that if i want the player to export your data with cloud or to other device, it's a problem for me.Here is my new code if anyone need! using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using System.IO;

 public static class SaveLoad {
 
     public static List<Game> savedGames = new List<Game>();
             
     //it's static so we can call it from anywhere
     public static void Save() {
         SaveLoad.savedGames.Add(Game.current);
         BinaryFormatter bf = new BinaryFormatter();
         //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
         FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
         bf.Serialize(file, SaveLoad.savedGames);
         file.Close();
     }    
     
     public static void Load() {
         if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
             Debug.Log(Application.persistentDataPath);
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
             SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
             file.Close();
         }
     }
     public static void Delete() {
         SaveLoad.savedGames.Remove(Game.current);
         BinaryFormatter bf = new BinaryFormatter();
         //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
         FileStream file = File.OpenWrite(Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
         bf.Serialize(file, SaveLoad.savedGames);
         file.Close();
     }    
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Danor · Sep 18, 2014 at 02:41 PM 0
Share

Hello! thanks for posting the new code, there is something i don't understand tho: you made a list for savegames and i can see it adds the current savegame as new save but what happens when there already is a savefile, you are writing it to the same path under the same name so won't it overwrite it or if it doesn't overwrite how would i let the player choose which saved game to load?

avatar image
1

Answer by Good-old-Grim · May 27, 2014 at 07:22 AM

Overwrite the file. Editing it in place is more trouble than its worth.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by fafase · May 27, 2014 at 07:30 AM

Is it really worth implementing your own system? Unity offers the PlayerPrefs class which stores info in the deep folder into registry which makes hacking slightly harder (just a slight bit).

Even though it only stores a limited amount of type (int, float, string), you could create your own parsing method to store it all into a string and parse it back into whatever type you need.

Then PlayerPrefs has a delete method that will erase all info.

Obviously, if you are after a learning process this does not apply.

Then you would have to load the file in a temporary string (or StringBuilder) then look for the starting index (index 1) of the object you are after and the ending index (index 2) of the line using IndexOf method.

Use substring to cut the string in two strings (start to index 1, index 2 to end) and join those two together, overwrite the file with the new file.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fornoreason1000 · May 27, 2014 at 08:38 AM 0
Share

I don't like storing data on the registry, it just seems unnecessary. not top mention registry cleaners from antivirus like AVG may wipe the save data (registry cleaners).

when you have about 120+ variables being stored. (each number of enemies killed, the stats of 13 characters(14 variables per character). registry storage is virtually out the window.

what i do is use an XOR encryption and save it to a folder in User/documents/CompanyName or whatever yet still moderately secure well far more that of the registry. still fast enough to auto-save very often.

this also allows for multiple saves. Playerprefs, from its name and description in the docs sound likes its more of a config file than a save system.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android APK deletes save game files on new install 1 Answer

Save Game loading Deserialization from disk 1 Answer

Do we have to delete old obb data when pushing updates? 1 Answer

"Asset Serialization Mode" - Is that only during development or runtime as well? 1 Answer

Serializer Script 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges