Unity 3D and Android, how to handle txt file?

Asked

Viewed 1,880 times

9

How to do in the Unity 3D read a arquivo txt line by line and store in a vector using C#.

Well, I managed to manipulate the arquivo .txt, however in Android it doesn’t work.

Follow the code I used.

int counter;
string line;
string[] teste = new string[10];
System.IO.StreamReader file;
file = new StreamReader("nome_do_arquivo.txt");

        while ((line = file.ReadLine()) != null)
        {
             teste[counter] = line;

             counter++;
        }

        file.Close(); 

This pc code solves my problem, however I need to Android, when compiling for Android that method doesn’t work, it’s like he didn’t take the .txt next to the Apk.

Another question, in the Unity 3D used C# for Android, how to manipulate txt files?

2 answers

10

According to the Documentation, you need a little gum call Resources.

Pathname of the target Folder. When using the Empty string (i.e., ""), the Function will load the entire Contents of the Resources Folder.

Now with this procedure you should be able to read the file on Android:

public static string Read(string filename) {
    //Load the text file using Reources.Load
    TextAsset theTextFile = Resources.Load<TextAsset>(filename);

    //There's a text file named filename, lets get it's contents and return it
    if(theTextFile != null)
        return theTextFile.text;

    //There's no file, return an empty string.
    return string.Empty;
}

And here in the Unity Forum a question with Validated Answer that may raise some doubts!

Edit:

Various ways to read each line in the file:

public TextAsset TextFile; //Com essa variavel você pode jogar o arquivo pelo Inspetor 
void readTextFileLines() { 
    string[] linesInFile = TextFile.text.Split('\n');

    foreach (string line in linesInFile) //Para cada linha....
    {
        //Aqui você adiciona no seu vetor!
    }
}      

Similar to what you use (which would be even easier for you to implement!):

using System.IO;

void readTextFile(string file_path)
{
   StreamReader inp_stm = new StreamReader(file_path);

   while(!inp_stm.EndOfStream)
   {
       string inp_ln = stm.ReadLine( );
       // Do Something with the input. 
   }

   inp_stm.Close( );  
}

And so on and so forth...

  • With it you can read the file, and yes, you can use part of yours to popular power the array! I am without pc with Unity here to go helping in the tests!

3


See this example that reads one line by line:

using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class LineReader : MonoBehaviour
{
    protected FileInfo theSourceFile = null;
    protected StreamReader reader = null;
    protected string text = " "; // permite que a primeira linha seja lida

    void Start () {
        theSourceFile = new FileInfo ("nome_do_arquivo.txt");
        reader = theSourceFile.OpenText();
    }

    void Update () {
        if (text != null) {
            text = reader.ReadLine();
            //Console.WriteLine(text);
            print (text);
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.