Create a txt file

Hi, I have a text file in my unity and when i build the game i want to be and that file, but no in “GameName_Data”
Example:

How i can do that?

Take a look at Streaming Assets.

If I’m understanding you right, you have a text file and want it to be in the executable file when built?

You need to add that file into the project and put it in the Resources folder like this:

Assets/Resources/myfile.txt

Then in the code you load it using this:

The .txt file you will be loading is an object of type “TextAsset”

void LoadFromResources()
{
    // load Template Files from the assets/resources folder
    UnityEngine.Object[] templateFiles = Resources.LoadAll("Templates/Terrain");
    int numberOfTemplates = templateFiles.Length;

    // each array element is an entire template file
    for (int i = 0; i < numberOfTemplates; i++)
    {
      Debug.Log("---- Loading Terrain Template :  " + ((TextAsset)templateFiles[i]).name + " ----\n");

      if (((TextAsset)templateFiles[i]).text == emptyString)
     {
       Debug.Log("ERROR :: Skipping Empty Template File from Assets/Resources/Templates/Terrain"+((TextAsset)templateFiles[i]).name + "  -is empty\n");
       continue;
     }

     kv.Clear();  // clear the List of key value pairs before accessing the next templateFile[i]

     //Split one File into lines, and drop empty lines, lines with spaces are kept, skip those later
     lines = ((TextAsset)templateFiles[i]).text.Split(splitFile, StringSplitOptions.RemoveEmptyEntries);

     ParseLines(((TextAsset)templateFiles[i]).name);  // use lines array  to put key/values into kv
     ProcessKV(); // use kv to make templates and add to dictionary
  }

}

I’m wondering what you want. You want to read a text? write a text file? you want the textfile to be in your datafolder?

If you want the textile to be in your datafolder make sure you place the textfile under Assets/Resources and use the code by @Tzan . If you want to read/write you need special code.

It sounds like OP isn’t wanting to use the Resources folder because they say “not in _Data folder”, and the screenshot shows the file in the root folder along with the executable.

For this you can use PostProcessBuildAttribute to define a method that gets called after your project is built. In this method you can use System.IO.File.Copy to copy your text file from a known location to the target location based on the pathToBuildProject parameter.

If thats the case he could load relative to the executable. I’m using this method to allow a texture pack being changed without having to go through he data folder.

string textfilelocation = Application.dataPath +"/../../Textfile.txt"

/…/…/ navigates all the way out of the data path to the directory holding executable and TestGame_Data folder. from here you could load your .txt file(atleast on OSX)

edit: the bonus using this method is that you will always go outside the relative dataPath and thus it will work on any location the files are.

edit2: note that on windows you might want to switch “/…/…/” with “/…/”

Source:

At build time Application.dataPath returns the path to the project Assets folder, so using it as the source for the copy at build time would be Application.dataPath + “/…/Textfile.txt”.

Putting a file into Resources, bundles it into the executable.
So it is not a separate file in the Data folder.

You only do this when you have a file that will not change after the project is built.

I have a txt file, i want just to be created in that folder with the TestGame.exe.

Okay if you want to write a file in the same folder as your exe.

using System.IO;

string dir = Application.dataPath + "/../"
string txtname = "textfile.txt";
string textPath;

void Start()
{
   textPath = Path.Combine(dir, txtname);
}

void DoSomething()
{
    using (StreamWriter writer = new StreamWriter(textPath, false))
            {
                writer.WriteLine("astring to write");
                writer.WriteLine("aInt.ToString() to write");
                writer.Flush();
            }
}

This should do the trick. but make sure you test. incase its placed in the root of _Data add an extra /…/ to the dir string.

also note that the boolean in the StreamWriter sets if the text in the file should be replaced(false) or added(true).
WriteLine writes a string to standard output stream(StreamWriter in this case)
I’m using Path.Combine to create the final URL. But you could also use a full string (Application.dataPath +“/…/textfile.txt”)

for the sake of referencing I’ve added the MSDN references to the text above so you could read about the functions :smile:

1 Like

Thanks :)), where i need to attach this script?
and, textPath? i need to add a string?, is not exist in script :))

attach the script to any object(unless you want to write variables from another script, then I advise you put this code in that script)

textPath = a string indeed. I’ve edited my code.