Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Zelethor · Jan 08, 2017 at 08:37 PM · editoriossave datafilepathbinarywriter

IOS File Path for saving data

Hi All

So been stuck for a while on this so any help is appreciated. Basically, the following code works perfectly in the editor to save currency data but on iOS builds, the data does not save. I think it has something to do with the save file path on iOS not being right because im not getting any errors. If someone could see where its going wrong in my code, it would be greatly appreciated as this problem is halting my release.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Security.Cryptography;

class LevelSave {

 private byte[] IV = { 0x6c, 0x1e, 0x85, 0x5e, 0x97, 0x4a, 0x9e, 0x39, 0x9b, 0x80, 0x33, 0x31, 0x5d, 0x76, 0x6e, 0xc5 };
 private byte[] Key = { 0x78, 0x06, 0x2f, 0x16, 0x3a, 0x5f, 0x4d, 0xcc, 0xe4, 0x28, 0xb7, 0x6f, 0x75, 0x1b, 0xd4, 0xb8 };

 void Awake (){
     Environment.SetEnvironmentVariable ("MONO_REFLECTION_SERIALIZER", "yes");
 }

 public void save(string toFile, ref List<LevelData> levels)
 {

     FileStream fileStream = new FileStream(toFile, FileMode.Create);
     RijndaelManaged RMCrypto = new RijndaelManaged();


     CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
     BinaryWriter writer = new BinaryWriter(CryptStream);

     for (int i = 0; i < levels.Count; i++)
     {

         writer.Write(levels[i].locked);

     }

     writer.Close();
     CryptStream.Close();
     fileStream.Close();

 }

 public void load(string fromFile, ref List<LevelData> levels)
 {

     if (!File.Exists(fromFile))
         return;

     try
     {

         FileStream fileStream = new FileStream(fromFile, FileMode.Open);
         RijndaelManaged RMCrypto = new RijndaelManaged();


         CryptoStream CryptStream = new CryptoStream(fileStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
         BinaryReader reader = new BinaryReader(CryptStream);

         for (int i = 0; i < levels.Count; i++)
         {

             levels[i].locked = reader.ReadBoolean();

         }

         reader.Close();
         CryptStream.Close();
         fileStream.Close();

     }
     catch (Exception e)
     {

         return;

     }

 }

}

Comment
Add comment · Show 5
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 agentc0re · Jan 09, 2017 at 12:03 AM 0
Share

In your code, I'm not seeing where you define the path where it will be saved. Is that in seperate code?

avatar image Zelethor agentc0re · Jan 09, 2017 at 02:14 AM 0
Share

Thanks for your reply! I'll have a look because i think it could be defined in another script, i'll have a look as soon as i finish work in 2-3 hours.

avatar image Zelethor agentc0re · Jan 09, 2017 at 05:23 AM 0
Share

O$$anonymous$$ i got the code where the file path is specified for saving/loading currency:-

using UnityEngine; using System.Collections; using System; using System.IO;

public class Currency : $$anonymous$$onoBehaviour {

 public static Currency instance;
 public string file = "currency.bin";//The file to save data in

 private int currency;
 private Encryption encrypter;

 void Awake () {
     Environment.SetEnvironmentVariable ("$$anonymous$$ONO_REFLECTION_SERIALIZER", "yes");

 }

 void Start()
 {
     encrypter = new Encryption();
     instance = this;
     load();

 }

 public void save()
 {

     encrypter.save(file, currency);

 }

 public void load()
 {

     currency = encrypter.load(file);

     if(currency == -1)
     {

         Debug.LogError("Issue loading file");
         currency = 0;

     }

 }

 public int get()
 {

     return currency;

 }

 public void add(int toAdd)
 {

     currency += toAdd;

 }

 public void remove(int toRemove)
 {

     currency -= toRemove;

 }

 public void addS(int toAdd)
 {

     add(toAdd);
     save();

 }

 public void removeS(int toRemove)
 {

     remove(toRemove);
     save();

 }

}

avatar image Yword · Jan 09, 2017 at 07:55 AM 0
Share

$$anonymous$$aybe you need to set the file path relative to Application.persistentDataPath.

 void Awake()
 {
     file = Application.persistentDataPath + "/currency.bin";
 }


avatar image agentc0re Yword · Jan 09, 2017 at 04:23 PM 0
Share

heh, you beat me too it. This is exactly what needs to happen! :D

However some rules need to be setup as on windows the path should include "file://" + ....

1 Reply

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

Answer by agentc0re · Jan 09, 2017 at 07:04 PM

These are just examples to show you what you could do as I pulled them from my own code and tried to make them geared towards what you need. Because if you're deploying this on multiple platforms you will need to check which platform you are on to create said directory correctly.

         private static string GetPathBasedOnOS()
         {
             if (Application.isEditor)
                 return "file://" + Application.persistentDataPath + "/";
              else if (Application.isWebPlayer)
                 return System.IO.Path.GetDirectoryName(Application.absoluteURL).Replace("\\", "/") + "/";
             else if (Application.isMobilePlatform || Application.isConsolePlatform)
                 return Application.persistentDataPath;
             else // For standalone player.
                 return "file://" +  Application.persistentDataPath + "/";
         }

Could also make a function that creates the directory. Something like this:

 static public string CreateDirectory()
         {
             // Choose the output path according to the build target.
             string outputPath = Path.Combine(GetPathBasedOnOS(), "YourGameNameSaveDir");
             if (!Directory.Exists(outputPath))
                 Directory.CreateDirectory(outputPath);
 
             return outputPath;
         }

You could also make this better by declaring your game name in a string variable and use that instead of the quotes with your game name in it.

Like I said, I altered this code to fit more of your situation. The original code works for me, you might have to tinker to make this work for you. :D

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 Zelethor · Jan 10, 2017 at 05:27 AM 0
Share

Thanks so much, this was the best answer so far. I amended the code and changed where 'toFile' and 'fromFile' were, to Application.persistentDataPath + "/mydata". Everything else works like a charm on iOS now and is correctly saving. Thanks again

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

114 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

While trying to install the iOS editor package separately the component gives an error in the end. 1 Answer

Unity 2018.2.0f2 i can't set the Target minimum iOS Version 1 Answer

Serialize Data on iOS in persistentDataPath 0 Answers

Background image is too bright on iOS, ok in Editor and Android APK 0 Answers

iOS build size is too large 0 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