Search Unity

How to work with external text files?

Discussion in 'Scripting' started by gustavopinent, Jul 24, 2019.

  1. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    My projects will be multi languages, and I wish to keep it accessible to the community to make translations. So in my first multi language game, I design a JSON schema and all text of my game is inside, including UI buttons, everything. I can even change the language on the fly, so I am satisfied with the result.

    I create the text file in this path inside Assets folder: "/UI/Textos.json". When loading, I just complete the path with Application.dataPath value and works fine in the editor's player.

    But when I build it for macOS, the text could not be found, in fact there is no "UI" folder in the app package. So I have some questions about:

    How to include the text file and make the right path so it can be found?
    If this is not an advisable method, how should I do?
    Is TextAsset a better choice? How to work with TextAsset?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    When Unity compiles the project it goes through all your scenes/prefabs/etc and see what resources are referenced and will include those in the built product.

    If there are assets that aren't directly referenced as an asset (for text this means a script somewhere referencing it as TextAsset, and you've configured some prefab/scene/etc to have this script and point at the text file). If you aren't referencing it anywhere, and you still want to force include it, you should put it in a 'Resources' folder (though Unity suggests resources folder should be avoided for the most part unless super necessary). These are then loaded via the Resources class:
    https://docs.unity3d.com/ScriptReference/Resources.html

    You could also always just force place the file into your built product afterward. Run your own custom script at the end of build and pack in your own installation stuff.
     
    gustavopinent likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    A json file is a TextAsset, so I don't understand your question. I don't know much about macs, but you can either stick the file in a Resource folder and use Resource.Load or put it in StreamingAssets and load from there, or just have a variable that you drag and drop the file into. For something like this particular file, I would probably choose the resource folder myself.

    Here are some examples of loading files, including json. https://docs.unity3d.com/ScriptReference/Resources.Load.html

    I also see that @lordofduct beat me to it.
     
    gustavopinent likes this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This path
    is path to the folder where your app can have write access to save it's data. Accidentaly on windows editor platform it happens to be the project folder itself. This coincidence confuses many, including you.
    In unity runtime player you can't load arbitrary assets from Assets folder of your project. Moreover, the build only ccontain the assets referenced from scenes so your text file placed somewhere in Assets is not even included in build.
    For working with arbitrary text file in build you have following options:
    1. Download it from the Internet using UnityWebRequest.
    2. Put it into Resources folder and load using Resources.Load<TextAsset>(...).
    3. Put it into StreamableAssets folder and load using UnityWebRequest for filesystem (see docs).
    4. Put it into StreamableAssets folder and load with .NET standard options (File class, various streams).
    5. Reference it directly in scene as TextAsset.
    6. Write a scriptable importer and create special asset type for localizations like a DictionaryAssset or whatever.
    7. Move it to asset bundle and use asset bundle workflow and API for loading as TextAsset or custom asset (see #6).
    8. Parse it and create ScriptableObjects with translations, reference them directly or load dynamically using 1-4
     
    Fenikkel and gustavopinent like this.
  5. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    I was not sure if I could deal with a text file as TextAsset just like this, never did...

    So reading all these posts I decided to incorporate the texts as TextAssets at list for now. It's easy and it works without any further interventions. The text won't change after the game initiates so there is no reason to load it on the fly. However, I will keep this post bookmarked because I will need it for sure in other cases such saving player's progress.

    I worked in other project translating the text to my language (ptBR). The text was inside an Excel file and it was very tricky to work, permission issues etc., will try to avoid this practice at list for now.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    To note, the folder name is StreamingAssets, and not StreamableAssets https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html
     
    StarManta and palex-nx like this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd place it in a StreamingAssets folder and use System.IO instead of TextAsset if you intend on users to be able to modify the language files of your build and not need your original Unity project. A TextAsset is part of the build itself, same with anything in a Resources folder, so aren't something users will be able to modify unless your project is being made open source.
     
  8. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    In this case will be open source, so I think is better to build a new version each time somebody contributes with a translation. Otherwise, yes it is better to load a text so the client can (if he wish) make his own translation in his own PC.

    Also, I am thinking about to keep the TextAsset but to make an option for to load an external translation so the user can test his own new text, but this will be for a much bigger project.
     
  9. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183

    If you want users to be able to edit or create new languages, I would highly recommend using the StreamingAssets folder. That way you don't have to re-build the game every time