1
\$\begingroup\$

I have a file which is created manually. In what folder should I put it so I can read it in a build?

For example Application.dataPath in editor reads the file from the assets folder, but if I put it there, will my code find that file in a build if I search it by Application.dataPath + folder name?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Can you tell us anything about the type of the file? If it's a text or binary file you want to read as strings or bytes, it might be best to set it up as a TextAsset, so you can store a reference to it the same way you reference Unity-native assets, and it's properly tracked in build dependencies. For more complex use cases, you can write your own importer to import it as an asset. \$\endgroup\$
    – DMGregory
    Commented Sep 11, 2022 at 15:58
  • \$\begingroup\$ It is json file but I guess the same logic than apply right or maybe should I use resources folder? \$\endgroup\$
    – Ivan
    Commented Sep 11, 2022 at 16:02
  • 1
    \$\begingroup\$ Resources is an old system. If you need dynamic loading these days, it's better to go with Addressables. But for JSON data, most often you can just reference it directly in a public or serialized TextAsset variable on your script. \$\endgroup\$
    – DMGregory
    Commented Sep 11, 2022 at 16:16
  • \$\begingroup\$ Would like to add, if you are developing prototype or game having only few small files to load, then you can use Resources. But not recommended for projects having number of files to load or large files. Alternatively you can also use StreamingAssets to load files. \$\endgroup\$ Commented Sep 12, 2022 at 6:13

1 Answer 1

2
\$\begingroup\$

If the file is modified at edit time, and not modified in the built version of the game, just put the file somewhere in your Assets directory like you do with other assets including textures, models, scripts, etc.

Files with the .JSON extension will automatically be imported as TextAssets. (The same goes for various other extensions listed at that link, including .txt, .bytes, .csv, .xml...) This lets you load data out of the file using the engine's native referencing features, without writing your own file IO code or hard-coding any specific paths.

In the script that needs to use this JSON data, just create a public or [SerializeField] variable of type TextAsset. That will give you a slot in the Inspector where you can drag this JSON file to be referenced.

Doing it this way, Unity's build process and loading systems will be able to see where this file is referenced from, and ensure it's loaded into memory when you load a scene or prefab that references it - so you don't need to worry about blocking or asynchronous file reading in your own code. It also means if you have gobs of these JSON files and some are out of date / unneeded / no longer referenced anywhere, the engine can detect that and strip them out so they don't add bloat or leak any development secrets in your build.

TextAssets also work with the Unity Addressable Assets system if you need to load it dynamically or stream it from an asset package hosted online.

You can then get at the JSON text in the file with myJsonTextAsset.text and use the JsonUtility or other library of your choice to parse it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .