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 Legolerp · Jan 18, 2019 at 03:08 PM · androidjsonloading file

Location for files in order to be able to load them on Android

I have made my levels for my game in json format. I have a class to load the json file and convert the json to a Serializable object which I in turn use to create a GameObject. My levels are under /Assets/Resources/Levels/ with a file name like *Level01-01.json". I've tried using both Application.dataPath and Application.persistentDataPath.

When I execute this on pc it works fine. When I try it on Android it can't find the file. Should I put my json files under a different directory? Should I build my FilePath in another way? Is the technique for reading the file wrong?

 using System.Collections.Generic;
 using System.IO;
 using UnityEngine;
 
 public class LevelDataController : MonoBehaviour {
 
     private string gameDataProjectFilePath = "/Resources/Levels/Level";
 
     public Level level;
     public string levelpacknr;
     public string levelnr;
 
     private string FilePath() {
         return Application.dataPath + gameDataProjectFilePath + levelpacknr + "-" + levelnr + ".json";
     }
 
     public void LoadGameData() {
         string filePath = FilePath();
 
         if (File.Exists(filePath)) {
             Debug.Log("file at " + filePath + " exists");
             string dataAsJson = File.ReadAllText(filePath);
             level = JsonUtility.FromJson<Level>(dataAsJson);
         } else {
             Debug.Log("file at " + filePath + " does not exist");
         }
     }
 }
 






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

2 Replies

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

Answer by Hellium · Jan 18, 2019 at 07:49 PM

JSON files can be handled as TextAsset in Unity, making things muuuch easier. The only thing you have to do is to move the JSON file from the Resources folder to another folder under Assets such as DataFiles for instance.

 public class LevelDataController : MonoBehaviour
 {
      // Drag & drop the file located in `Assets/DataFiles/Levels`  
      // into the following field of the inspector
      [SerializeField]
      private TextAsset gameData = null;
  
      public Level level;
      public string levelpacknr;
      public string levelnr;
  
      public void LoadGameData()
      {
          string filePath = FilePath();
  
          if ( gameData != null )
          {
              string dataAsJson = gameData.text ;
              level = JsonUtility.FromJson<Level>(dataAsJson);
          } else
          {
              Debug.LogError("You forgot to drag & drop the JSON file into the inspector");
          }
      }
  }
  





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 Legolerp · Jan 18, 2019 at 08:17 PM 0
Share

I have every level in a different file. Should I join them in one json file or van I have an array of TextAsset's? Your solution does seem easier not having to worry about which path to use.

Thanks for the reply already, gonna try this out when I get access to my pc :)

avatar image Hellium Legolerp · Jan 18, 2019 at 08:48 PM 0
Share

I believe having an array of TextAsset is the way to go !

avatar image Legolerp Hellium · Jan 18, 2019 at 11:10 PM 0
Share

I worked it out like you suggested with TextAsset + drag and drop. Now I can use it on my pc and on Android. Thank you so much!

avatar image
0

Answer by xxmariofer · Jan 18, 2019 at 04:17 PM

you need to use WWW for accessing content in android since is inside jar, also see documentation for deciding if you should use persistentdatapath or streaming assets. check https://answers.unity.com/questions/1231211/how-to-get-resources-filepath-on-android-device.html

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 Legolerp · Jan 18, 2019 at 06:04 PM 0
Share

And can I keep my json files under Assets/Resources/Levels?

avatar image xxmariofer Legolerp · Jan 18, 2019 at 07:23 PM -1
Share

I cant test it now and to be honest, i have only used WWW for retrieving info from inside the jar but the info was downloaded at runtime so i am not sure if you have to place it at one exact folder at build, i just downloaded a zip in a random folder inside persistentdatapath directory and could access it.

avatar image Legolerp xxmariofer · Jan 18, 2019 at 08:19 PM 0
Share

Thanks for the reply, I'll test it next time I have access to my pc :)

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

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

Cannot write data into JSON file on Android's external memory 1 Answer

can you read json file on android ? I have a thesis and it's not going well. 1 Answer

File.ReadAllBytes() returns byte[0] 0 Answers

Help Writing and reading files on android. 1 Answer

Ressource.Load returns null on "build" but work on "build and run" 2 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