- Home /
How do I access files in the file system using methods other than inheritance and variables?
*Sorry, this is a very basic question, I'm new to unity and my course hasn't covered this topic yet.
How do I access files in the file system using methods other than inheritance and variables?
For example, say I want to give one GameObject access to another GameObject in the script, how would I do that in the code (not using public or [SerializeField] variables)?
For example, say there is a spawn manager, that needs to spawn many different objects. How can I just give the spawn manager access to a folder, so that it can access everything inside, rather than using a ugly and time consuming public list?
Answer by Bunny83 · Aug 06, 2021 at 12:11 AM
Your title and question was confusing at first. However I think I get where your confusion comes from. Inside the Unity editor you manage your assets as "files" in folders. However the build game does not have any of those files at all. From the engines perspective it just works with assets. Assets are registrated in the asset database by a GUID. When you build a game, only "used" / referenced assets will be included in your build. As I said the folders you may have in your project simply do not exist in the build game.
There are several possible solutions, however some of them highly depend on what kind of assets you have.
The usual and recommended solution: References
The best solution in general is to have serialized references to your assets. You said that a public List is
ugly and time consuming
however this may just be that you don't know how to work with Lists in the editor. Let me include one of my canned gifs that should explain an important concept in the editor: Locking the inspector
The Resources folder(s)
Another option is to use Unity's concept of Resources folders. Any assets in folders named Resources will automatically be included in the build. You can use the various Resources.Load functions (See the Resources class for more details.). Note that an asset path to an asset in a resources folder have to be a relative path that starts inside the Resources folder. An asset path uses forward slashes and you must omit the file extension of the the actual asset. Note that Unity does not provide any tools at runtime to "search" or browse the files in the resources folder. The only methods that could do "blind loading" are the LoadAll method variants. However they literally load all assets under the specified path. A workaround is to build your own "dictionary" at edit time which you can consult at runtime. I made such a workaround some time ago. For more information see this UA question.
Besides the Resources folder there are also AssetBundles and the new Addressables system. While AssetBundles behave a bit like a plugable resources folder, Addressables are more flexible. However it requires quite a bit of setup. Also those are mainly meant to provide external precompiled assets which are loadable at runtime.
Finally there's always
The custom solution
Unity provides many functions to load certain kind of asset types at runtime from actual source files. However this is very limited. You can load images (currently only jpg and png are supported), AudioClips (only wav and ogg are supported in windows, on Android mp3 is supported for background music through the hardware streaming support). Unity does not (yet) provide any support to load models, animations or other complex data, however you can of course always implement such a thing yourself. I've written bmp and gif loaders from scratch and also a loader for the cal3d model format.Of course all of the above mentioned methods could be supported through some clever editor scripting. You can always write some editor script that does certain pre-processing for you, a bit like my ResourceDB that I mentioned above.
Thanks! Sorry, I thought you had to manually add each item to the list. Plus, the resources looks just like what I was looking for! One more question, can you convert a folder into a list for easy accessing? Is this possible?
Your answer
![](https://koobas.hobune.stream/wayback/20220613041252im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers