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
2
Question by sathya · Jul 02, 2014 at 11:24 AM · jsonexternal files

Saving a json file in resource folder

Hello Fellow Developers,

There is a very peculiar problem that I am facing. The code part as under is what I am using to save my Item Info in a JSON file (I am Using SimpleJSON for JSON operation).

My information file is getting saved but the changes are not reflected in the file till I minimize unity editor and reopen it again.

 public void SaveItemInfo()
     {
           string str = ItemInfo.ToString();
           using (FileStream fs = new FileStream("Assets/Resources/GameJSONData/ItemInfo.json", FileMode.Create))
           {
                    using (StreamWriter writer = new StreamWriter(fs))
                 {
                   writer.Write(str);
                   writer.Close();
                   writer.Dispose();
                 }
             fs.Close();
             fs.Dispose();
         }
     }

I know this sounds weird, I will try to explain the situation with a scenario. I make an in-game purchase and save the purchase info in a JSON file. When I launch the game next time I should the see the purchased item in my inventory but I do not see the changes. [No Changes made] But when I minimize Unity Editor and relaunch the game I get expected results.

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 ajketan · Jul 02, 2014 at 11:46 AM 0
Share

I too am facing a similar issue ... please can someone help ? I am developing on $$anonymous$$AC (if that is relevant)...

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by fafase · Jul 02, 2014 at 12:16 PM

You need to refresh the Editor so that the new item is considered. Qlso you do not need to call Dispose and Close as it is implied in the using(object) statement:

 public void SaveItemInfo(){
     string path = null;
     #if UNITY_EDITOR
     path = "Assets/Resources/GameJSONData/ItemInfo.json";
     #endif
     #if UNITY_STANDALONE
         // You cannot add a subfolder, at least it does not work for me
         path = "MyGame_Data/Resources/ItemInfo.json"
     #endif
  
     string str = ItemInfo.ToString();
     using (FileStream fs = new FileStream(path, FileMode.Create)){
         using (StreamWriter writer = new StreamWriter(fs)){
             writer.Write(str);
         }
     }
     #if UNITY_EDITOR
     UnityEditor.AssetDatabase.Refresh ();
     #endif
 }

You also need to add the using UnityEditor; at the top. The directive is necessary since that won't do in the final build.

Comment
Add comment · Show 3 · 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 ajketan · Jul 02, 2014 at 12:30 PM 0
Share

Thanks a lot FAFASE this works out of the box for me in the Editor..... But this will be used in the final build so we can not use the editor specific script. And also on deploying it onto the device I am getting the same issue...

avatar image fafase · Jul 02, 2014 at 01:06 PM 0
Share

I reviewed the answer including all kind of debug/build things. It works for me in both cases. As a side note, I would not store this kind of info in a simple json file. This is too easy to modify or read, PlayerPrefs is already a little better, PlayerPrefs with encryption is one step higher and storing on a database may be one of the best solution.

avatar image ajketan · Jul 03, 2014 at 12:48 PM 0
Share

Thanks fafase.... works for me now

avatar image
2

Answer by Trung-Hieu · Jun 05, 2016 at 04:16 PM

You may consider saving using PlayerPrefs. It is basically the same as writing into the text file and store on the client.

However when it comes to purchases, it would be better to have a server to store the information somewhere. Storing purchases or sensitive information on client is:

  • Not secured and can be easily hacked.

  • Players may uninstall the game and reinstall them, which will cause them to lose their purchased items

:)

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I call a File outside Unity without the Location? 3 Answers

Running external program from c# code 0 Answers

Json . NET, iOS and AOT 1 Answer

How do I get a number out of a JSON file with JSONObject.cs 1 Answer

Import oculus configuration unity .json file 1 Answer


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