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 /
avatar image
0
Question by lambdastudios · Dec 28, 2016 at 10:54 PM · androidloadingjsonsaving

JSON file not written/read on application exit on android

Hi everyone in my game im trying to save a json file in the OnApplicationQuit() and load it in the start method. This works perfectly in the unity editor but, when I run the game on an android device the file isn't written or maybe not read. Is OnApplicationQuit() not called on android? Or do you have to read JSON differently on Android?

This is were I load the JSON file:

 public static string filePath = Application.persistentDataPath + "/gamestats.json";
 
     public static GameStats loadStats(GameObject[] defualtBlocks)
     {
         GameStats stats = new GameStats ();
         stats.blockDatas = new BlockData[defualtBlocks.Length];
         if (File.Exists (filePath))
         {
             string sourceJSON = File.ReadAllText (filePath);
             stats = JsonUtility.FromJson<GameStats> (sourceJSON);
         }
         else
         {
             for(int i = 0; i < defualtBlocks.Length; i++)
             {
                 stats.blockDatas [i] = defualtBlocks [i].GetComponent<Block> ().blockData;
             }
         }
         return stats;
     }

This is were I save the JSON File:

     public static void saveGame(GameStats stats, GameObject[] allBlocks)
     {
         Debug.Log (allBlocks.Length);
         for(int i = 0; i < allBlocks.Length; i++)
         {
             stats.blockDatas [i] = allBlocks [i].GetComponent<Block> ().blockData;
         }
         string gameStatsJSON = JsonUtility.ToJson (stats);
 
         File.WriteAllText (filePath, gameStatsJSON);
     }

And I call loadStats in the Start() method and saveGame() in the OnApplicationQuit() method.

Thanks for your help in advance!!!

Comment
Add comment
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

1 Reply

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

Answer by $$anonymous$$ · Dec 29, 2016 at 01:48 AM

First check if file is created on your android device, and if it exits, does it have something, mb its empty.

On android you should create files in a different way. I use this method, it works for me. I thinks there is nothing too complicated to adapt this for you.

  if (Application.platform == RuntimePlatform.Android)
         {
             if (file == "")
             {
                 currentSetting = new Setting(new RandomStats(0.25, 0.5), new RandomStats(0.66, 1.0), new RandomStats(0.66, 1.0),1000,0);
                 
                 string filepath = Application.persistentDataPath + "/" + "Settings" + ".json";
                 Debug.Log("Running system");
                 if (!File.Exists(filepath))
 
                 {
 
                     WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "Settings" + ".json");
                     //TODO A coroutine instead of this dangerous cliff
                     while (!loadDB.isDone) { }
                     File.WriteAllBytes(filepath, loadDB.bytes);
                 }
                 File.WriteAllText(filepath, PrettyPrint(currentSetting));
             }
             else
             {
                 currentSetting = JsonMapper.ToObject<Setting>(json_settingsData.ToJson());
             }
 
         }
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 lambdastudios · Dec 29, 2016 at 03:32 AM 0
Share

Hey Le_Vailus thanks for the answer and im sure your way would have worked here but I put my save call somewhere else ins$$anonymous$$d of OnApplicationQuit() and it worked perfectly. For some reason OnApplicationQuit() isn't called on Android.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving and loading to/from Android 1 Answer

Saving frequently throughout game lifecycle (Android) 1 Answer

Instancing ScriptableObject at Runtime, loading and saving via JsonUtility 1 Answer

Saving And Loading on Android 0 Answers

Trying to save and load from a text file on android. 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