unity won't reconize my text files as text assets

Currently I am able to achieve this but in a very inefficient way.

private string[] allLines;
void CompileLines()
    {
           allLines = File.ReadAllLines(Dialogue.name.ToString() + ".txt");
    }

The problem that I am having is that I want to read from the actual text asset “Dialogue” rather than just getting its name and loading from the top of the project folder hierarchy.

2881397--211579--Capture.PNG

Is there a way to do this without changing too much code?
EDIT: Solved but more problems below

You just want to read a text file included in your project? I think the Streaming Assets Folder is what you are looking for.

I now have that set up and everything is working beautifully. I have another question if you don’t mind answering. Whenever I edit a .txt file in visual studios unity no longer will let me drop it in the inspector as a text asset and will proceed to open the file in notepad instead if I try to reopen it again. How can I fix this?

Unity uses the operating systems default file associations, so if a .txt file opens in notepad, thats because .txt files are associated with notepad on your system. You could change this to visual studio if you wanted, but then all .txt files you open on your system will open with vs.

I don’t understand what you mean by “unity no longer will let me drop it in the inspector as a text asset”?

I am just as confused. Whenever I hit save in VS the text file is no longer recognized as a text asset and cannot be dropped in the inspector as such. The file also loses its preview when I just click on it in the folder. When I try to open the text asset again by double clicking it from the “Streaming Assets” folder of my project, rather than defaulting to VS it opens up in notepad.

2882102--211644--Inspector.PNG 2882102--211645--Project.PNG
2882102--211646--Script.PNG

bump

Upon further investigation I noticed that if I moved a text file from my desktop to the “Resources” folder the file behaves but If I do the same but move it to the “StreamingAssets” folder the file is not recognized. When I move a file from “Resources” to “StreamingAssets” the file is not longer recognized and if I try moving it back to the “Resources” folder the file is still unrecognized.

Can you replicate this in an otherwise empty project? Sounds like either you have some scripts that are messing with the import, or a bug in Unity.

This can be easily replicated in a new project.
Steps to replicate:

  1. Create new project and add a resources and StreamingAssets folder
  2. Create a text document and drag it into the resources folder from your desktop
  3. open the text document from the resources folder and edit it then save it
  4. drag the text document from the resources folder into the StreamingAssets folder
  5. edit the text document again and save
  6. click the text document in the project and it breaks
    2882515--211667--Capture.PNG

Currently I have gone around the problem by doing this:

        allLines = Dialogue.text.Split('\n');

        for(int i = 0; i<allLines.Length; i++)
        {
            allLines[i] = allLines[i].Remove(allLines[i].Length-3, 2);
        }
1 Like

Sounds like a bug in Unity then, you should report it using the bug reporter.

1 Like

Have you tried to replicate this on your end? I want to make sure that the problem is not only on my end before I submit anything.

Works fine here, but I’m on a mac, could be a windows issue, or specific to your system in some way.

Never feel bad about reporting bugs, they are just happy for every bug report they get. Seriously.

1 Like

Alright I sent the bug report. Thanks for your help.

I don’t think this is a bug, I think its by design.

StreamingAssets is meant to be used for raw files that are copied to your build verbatim. You then load them via script. Since the contents of the file are copied directly, they don’t actually get included in the build. Since they aren’t included in the build, you can’t reference them in a scene.

If you want to use a scene reference, take it out of streaming assets, and treat it as a TextAsset. Otherwise put it in streaming assets and treat it as a file.

3 Likes

why can’t the official documentation be as straightforward as you. This actually explains a lot and tells me that my “workaround” from before is actually what I needed to do. I am still confused as to why this worked on Steego’s end.

1 Like

Yeah of course @Kiwasi is right that you won’t be able to drag and drop it to an inspector field as a text asset.

I didn’t check that, I just checked that I still got a preview of the contents of the file in the inspector, which I do.

You should be using File.ReadAllLines and the streaming asset path in this case.

Just to be clear here - the files are included in builds! They’re just not imported by Unity in any way. But stuff you put in StreamingAssets when you build are still in the folder after the build.

@Collin_Patrick_1 , if you want to work with text files as TextAssets, just put them in a normal folder (not StreamingAssets or Resources).

1 Like