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.

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

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.

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?