Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Load txt on Android from StreamAssets folder

Discussion in 'Android' started by Joishi, Dec 14, 2015.

  1. Joishi

    Joishi

    Joined:
    May 9, 2015
    Posts:
    7
    Hello, I´ve been trying to load an txt document, I tried differents ways, but none works(I found some post, but almost all were from two or more years ago). The Idea is to have different txt files with the dialogues in differents languages, to change the texts dynamically.
    I readed lot of posts but none have a real solution to this, or I didn't understand it. (by the way sorry for my english, I´m not native speaker)

    The things that I already know:
    -apk file is like a zip file and is necessary to use WWW class to read it(Unity documentation)

    Things I need to know:
    -Which path to use? Application.streamingAssetsPath, Application.persistentDataPath, Application.dataPath. The Unity document say that is necessary to use DataPath on android Devices and WWW class to open it.


    -Is the "!" symbol on the path necessary?, and what is his function?(I know it in coding("negation operator" !), but not in a path)

    Note: I´m testing in a new apk file, to prevent ruin the real app code


    This is the base code I have been using, I changed it a lot of times, but didnt work :
    Code (CSharp):
    1.  
    2.    string textoPagina = "";
    3.  
    4.     void Start () {
    5.            StartCoroutine(read());
    6.     }
    7.  
    8.     IEnumerator read() {  
    9.         string rpath = Path.Combine(Application.streamingAssetsPath, "test");
    10.         //string path = "jar:file://" + Application.dataPath + "/txt/test.txt";
    11.        
    12.         WWW www = new WWW(rpath);
    13.         yield return www;
    14.              
    15.         StringReader streamReader = new StringReader(www.text);
    16.         textoPagina = streamReader.ReadToEnd();
    17.         Text texto = GetComponentInChildren<Text>();
    18.         texto.text = textoPagina;
    19.        
    20.     }
    21.  
    "texto" is the button text that I want to change the text dinamically to test it.
    I have two documents "test" one in a txt folder, and other one on StreamingAssets folder, both have different frases to know which one was loaded
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Joishi likes this.
  3. Joishi

    Joishi

    Joined:
    May 9, 2015
    Posts:
    7
    Thanks, is working at the momment,