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 MOJAJA · Aug 16, 2019 at 07:14 AM · androidjsonpersistentdatapath

About Android can't load json file in Application.persistentDataPath

Hi all,

I put json file in Application.persistentDataPath,then It's can load and work on PC.

But It can't work in Android ,I'm stuck for a long time.

please help me , thank you!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 
 
 
 
 public class QuizCon : MonoBehaviour
 {
     public RoundData[] allRoundData;
     private string gameDataFileName = "quiz.json";
 
 
     private void Start()
     {
        
         DontDestroyOnLoad(gameObject);
         LoadGameData();
      
     }
 
   public RoundData GetCurrectRoundData()
     {
         return allRoundData[0];
     }
 
  private void LoadGameData()
     {
         string filePath = Path.Combine(Application.persistentDataPath, gameDataFileName);
         if(File.Exists(filePath))
         {
             string dataJson = File.ReadAllText(filePath);
             GameData loadedData = JsonUtility.FromJson<GameData>(dataJson);
 
             allRoundData = loadedData.allRoundData;
         }
         else
         {
             Debug.LogError("Cannot load game data!");
         }
     }
    
 
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

Answer by Bunny83 · Aug 16, 2019 at 09:47 AM

Well you said:

I put json file in Application.persistentDataPath

Did you actually put the file there on your android device? Usually you don't have access to that folder from outside your own application. So the only app that is able to store anything in that folder is your own app. The persistent data folder is always empty when you install an app. You can not ship files already in that folder. The folder is for persistent storate on the device, it's not a data-shipping folder. This folder is created by the Android OS for your app. When you install an app, everything that belongs to your app is within the APK file. Android does not install other files for you.


If you want to ship a json file with your app, you need to actually include it in your game files. A common way is to place the file in the streaming assets folder. You would still try to read the file from the persistent folder first. If there is no file, you would use w UnityWebRequest to retrieve the content from the streaming assets and then store it yourselt in the persistent folder.


If the json data is not meant to be changed by your application but only read you can directly load it from the streaming assets through the UnityWebRequest. Note: The streaming assets folder for most build targets is an actual folder within your data folder that is shipped as actual folder to the user. However on Android as mentioned everything is packed into the APK file (which essentially is a zip file). So you can not use the usual System.IO.File stuff. The UnityWebRequest and the WWW class can use a special URN which is returned by Application.streamingAssetsPath

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 MOJAJA · Aug 16, 2019 at 10:34 AM 0
Share

oh ! I got the wrong . I put the file in Application.persistentDataPath on Windows.

So , my android device don't have the file.

At first , I put the json file in folder of Strea$$anonymous$$gAssets ,then I use Application.strea$$anonymous$$gAssetsPath ,it's still work . When I build the apk and test on android, it can't work.

I'm confusing now . Application.persistentDataPath can't build in apk?

avatar image MOJAJA · Aug 19, 2019 at 06:19 AM 0
Share

Hi , I solve the problem. I put the file in Strea$$anonymous$$gAssets , and use UnityWebRequest to get the file ,thank you for your suggestion.

avatar image goodgamershow MOJAJA · Jun 08, 2021 at 10:22 AM 0
Share

Hi, @MOJAJA ! Did you use the code from below to access Strea$$anonymous$$gAssets folder with UnityWebRequest? var _path = Application.strea$$anonymous$$gAssetsPath + "/jsonFile.json"; UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(_path); www.SendWebRequest(); while (!www.isDone) { } String jsonString = www.downloadHandler.text; If yes, there's one more question: where should I put this code? Should I create an empty gameobject in my main menu(because the very first scene to load in my game is Main Menu) and attach a script to it, where in Awake() function I put the code from above?

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

240 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 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 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

Problems when saving and loading with FileStream 1 Answer

Application.persistentDataPath returning null on certain Android devices 2 Answers

Save via XML or JSON for mobile game 2 Answers

Saves on android, could not find file 0 Answers

How to send an Email with the details entered in a json file? 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