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 gallegomateo8 · Nov 27, 2018 at 05:59 PM · androidunity 5jsonworkdoesnt

Unity can read Json file, but Android not.

My json works on Unity, but when I try to build to Android, I can't load any questions. here is my code, I can't understand what it's going wrong.

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; using UnityEngine.Networking; using UnityEngine.SceneManagement;

public class QuestionManager : MonoBehaviour {

 public Text debugLog;
 public ShowToast androidOutput;
 public Text pregunta; 
 public Button r1;
 public Button r2;
 public Button r3;
 public Button r4;
 public Text textoTiempoRestante; //Texto donde se muestra el tiempo que te queda
 public int q = 1;           // 1-25 Varaible entera que sera el numero de la pregunta actual 
 public static int Length = 25;     // 1-25 Variable entera define el numero total de preguntas
 public Text textScore; //El texto lo aplicamos al Score de la UI
 
 public static int score; //= 0;
 private QuestionCategories m_categories; // Donde se almacenan la categorías
 
 private float tiempoRestante = 15f; //Tiempo para contestar la pregunta
 
 

 



 void Update() //Se ejecuta cada frame
 {
     textScore.text = "Score: " + score;
     tiempoRestante -= Time.deltaTime; //Restame del tiempo, un segundo cada segundo.
     textoTiempoRestante.text = "" + System.Math.Round(tiempoRestante, 1); //Escribeme en la pantalla, el tiempo que me queda. Y le digo que solo me muestre un decimal.
     if(tiempoRestante < 0) //Si el tiempo es inferior a 0, ejecutame el siguiente codigo.        
     {
          Debug.Log("¡Tiempo agotado!"); //Si se acaba el tiempo, sale ese mensaje en la consola de UNITY unicamente.
         androidOutput.showToastOnUiThread("¡Tiempo agotado!"); //Si se acaba el tiempo, sale ese mensaje en el telefono móvil.
        
         if (Length < q)          // si q es mes gran que Length significa que no hay mas preguntas
     {
         SceneManager.LoadScene("Ranking");      // en este caso cambia la escena a Ranking   
     }
         
         LoadQuestion(); //Cargame una pregunta nueva.
         tiempoRestante = 15f; //Añademe 15s al tiempo restante para la siguiente pregunta.   
     }

     
     
 }

 private void Awake() // Se ejecuta al empezar el juego
 {
     //Chequea o comprueba las respuestas al pulsar 
     r1.onClick.AddListener(delegate {CheckAnswer(r1); });
     r2.onClick.AddListener(delegate {CheckAnswer(r2); });
     r3.onClick.AddListener(delegate {CheckAnswer(r3); });
     r4.onClick.AddListener(delegate {CheckAnswer(r4); });
     // Per validar els vostres JSON si son correctes: https://jsonformatter.curiousconcept.com/
     ReadFile("SampleQuestions.json"); // Esto lee el archivo de las preguntas (json)

 }

 // COSAS NASIS DE ANDROID TETE
 private void ReadFile(string fileName)
 {
     // Explicació del que son els StreamingAssets: https://docs.unity3d.com/Manual/StreamingAssets.html

     string filePath;

     // Explicació del que es el PlatformDependentCompilation: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
     #if UNITY_EDITOR
     
         filePath = Path.Combine(Application.dataPath + "/StreamingAssets", fileName);
         MostrarLog(filePath);

         if(File.Exists(filePath))
         {
             string dataAsJson = File.ReadAllText(filePath); 
             m_categories = JsonUtility.FromJson<QuestionCategories>(dataAsJson);
             LoadQuestion();
         }
         else
         {
             MostrarLog("Cannot load game data!");
         }

     #elif UNITY_ANDROID

         filePath = Path.Combine("jar:file://" + Application.dataPath + "!/assets/", fileName);
         StartCoroutine(LoadAndroidFile(filePath));

     #endif
     
 }

 IEnumerator LoadAndroidFile(string filePath)
 {
     // Explicació del que son les Coroutines: https://unity3d.com/es/learn/tutorials/topics/scripting/coroutines?playlist=17117
     // Explicació del que es la classe WWW: https://docs.unity3d.com/ScriptReference/WWW.html

     WWW loadFileRequest = new WWW(filePath);
     yield return loadFileRequest;
 
     string dataAsJson = loadFileRequest.text;
     MostrarLog(dataAsJson);

     m_categories = JsonUtility.FromJson<QuestionCategories>(dataAsJson);
     
     LoadQuestion();
     

 }
  
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

Answer by Legend_Bacon · Nov 27, 2018 at 06:07 PM

Hello there,


You may want to debug the Path you are using on android (make a text somewhere on screen, and feed it the path you are using).


I don't think you need the Path.Combine() call... Here is an example of some of my working code:

 path = "jar:file://" + Application.dataPath + "!/assets/version.json"



I hope that helps!

Cheers

~LegendBacon

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 Bunny83 · Nov 28, 2018 at 05:46 AM 0
Share

Note that the strea$$anonymous$$g assets for android are packed inside the AP$$anonymous$$ file and can not be accessed directly through file IO. Unity's WWW class recognises this special URL scheme (jar:) and do some zip magic behind the scenes to load a file from inside the AP$$anonymous$$. I recommend to have a look at the last paragraph of the Strea$$anonymous$$gAssets documentation


ps: Another common reason for things that work on windows and doesn't work on android is case sensitivity of file and pathnames. On windows file and pathnames are case insensitive. So "C:\$$anonymous$$yFolder\SomeFile.txt " is the same as "C:\mYfOLDER\sOmEfiLe.txT ". Since Android is a linux based system, file and path names are case sensitive. So the casing of the names have to match exactly. This is also a common issue when setting up a website on a local hosted windows server and then moving onto an actual webserver which in 99% of all cases run on linux.

avatar image
1

Answer by mguidry · Nov 27, 2018 at 06:44 PM

I am pretty sure you have to use Application.persistentDataPath instead of Application.dataPath for Android platform.

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

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

Related Questions

Windows Phone 8.1 WWW post request error 0 Answers

Terrain and Lights doesn't work on Android 1 Answer

Android back button not working 2 Answers

How to save and load a json file on android 1 Answer

Unity project on android - OnPointerDown/Up movement 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