Monday, September 12, 2022
HomeGame Developmentc# - The place ought to I put a JSON file to...

c# – The place ought to I put a JSON file to be readable in a Unity construct?


If the file is modified at edit time, and never modified within the constructed model of the sport, simply put the file someplace in your Property listing such as you do with different belongings together with textures, fashions, scripts, and so forth.

Information with the .JSON extension will mechanically be imported as TextAssets. (The identical goes for varied different extensions listed at that hyperlink, together with .txt, .bytes, .csv, .xml…) This allows you to load information out of the file utilizing the engine’s native referencing options, with out writing your personal file IO code or hard-coding any particular paths.

Within the script that should use this JSON information, simply create a public or [SerializeField] variable of kind TextAsset. That offers you a slot within the Inspector the place you possibly can drag this JSON file to be referenced.

Doing it this manner, Unity’s construct course of and loading programs will be capable to see the place this file is referenced from, and guarantee it is loaded into reminiscence whenever you load a scene or prefab that references it – so that you need not fear about blocking or asynchronous file studying in your personal code. It additionally means when you have gobs of those JSON recordsdata and a few are outdated / unneeded / not referenced wherever, the engine can detect that and strip them out so they do not add bloat or leak any growth secrets and techniques in your construct.

TextAssets additionally work with the Unity Addressable Property system if you could load it dynamically or stream it from an asset package deal hosted on-line.

You may then get on the JSON textual content within the file with myJsonTextAsset.textual content and use the JsonUtility or different library of your option to parse it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments