- Home /
Open all files in a directory
Hi, I have a folder in my computer which has a lot of prefabs. I need to access then in one Unity project and instantiate some of these prefabs (according to the user's choice). I know that exists Resources.Load, but I can't use it. In fact, this folder is not static, so this prefabs may change and I opt for access a folder in my computer, but not in the Unity resources folder. So, I have to read all the prefabs in the folder and make then available. How could I do this? It would be using WWW?
Thanks.
Answer by Bunny83 · Apr 11, 2013 at 09:08 PM
The short answer is:
"Assets" in Unity can only have 4 different formats that can be read at runtime:
Assets built into the application. That are all the usual assets, no matter if referenced by a scene / script or if they have been placed into a Resources folder. All those assets are compiled into the application itself. They come with it and can't be used / accessed somewhere else. They are stored in a special format which can only be read by a Unity-player or the editor.
Assetbundles are external asset collection files which can be loaded at runtime from an arbitrary place you have access to (local file system, a webserver, ...). Assetbundles are a Unity pro feature. They have to be created within Unity.
External textures. You can load external textures with the WWW class and use WWW.texture to access it. Only the JPG and PNG format is supported.
MovieTexture. This is also a Unity pro feature. You can only load movies in the "Ogg Theora format". They can be loaded using WWW.movie
That's all Unity can access at runtime. You can not simply load an asset at runtime. Of course you can implement your own loader and your own custom data format, but Unity itself doesn't support loading any other assets out-of-the-box.
There are a lot third party solutions to save / load assets at runtime like the well known UnitySerializer
Nice, good to know.... no magic here. You have destroyed my answer :P
Answer by Sisso · Apr 09, 2013 at 07:48 PM
Resource is used only to read objects in our /Resource folder, that is automaticy embedded in your binary. You could use AssetBundle from Unity Pro or WWW.
My guess is that you need to do it in two steps.
First, access an already know url to fetch a list of prefabs. This could be a simple file or a complex json. You must put everything that user will need to make his choice, and the real url. You can easily generate it automatically from your prefabs inside Unity3d.
Second, load each selected prefab one by one using its real url. But like Bunny83 said, you will need a custom serialization because prefabs could not be loaded by WWW.
EDITED: Bunny83 confirm that you could not load prefab using WWW.
Answer by frarees · Apr 09, 2013 at 07:41 PM
AFAIK the only way to read GameObjects at runtime is by either referencing them via inspector, placing them into the hierarchy or searching them inside a Resources (sub)folder.
Is there any good reason not to use the Resources folder in your case?
If you want to "stage" files outside the Resources folder, there's no reason you couldn't copy IN TO the resources folder and then Resources.Load them after the copy completes.
I have a project that has to access a directory in runtime, and I can add models to this directory and click on a button to reload all models in it. So I CAN'T have these models in to the resources folder, or I would have to make a new build every time a change occurs.
If I understand what you're saying, that's not true. You'd click refresh, see that there's new files in "remote directory", copy the new ones to "resources directory" and then use a resources load.
So are you saying that I can copy these files to the resources folder in runtime? If so, it would be great, I didn't know that.
Yes - it'll require methods that are in the C# framework, not the Unity framework, but you have access to them through the 'System.IO ' namespace. Note that mobile platforms don't allow this type of manipulation (at all) but I don't suspect you're doing this sort of manipulation on mobile. I assumed PC.