0
\$\begingroup\$

I am directly download some object file (3D Models) inside resource folder and the using Resource.Load I am loading those model in unity. This work perfectly but the problem is I have to refresh the resource folder in order to load resource form there! The first step is downloading the asset then instantiate it through resource.load method. But I am falling in the last step as model import is required which is fine in the editor using assetdatbase refresh but this is not available in runtime.

Edit: I am download obj files in resource and then loading it from resource which is much faster then Runtime Obj Importer. Because when I am loading file from persistant path I have to use obj Loader method which is not async is freezing my main thread. Therefore i am preferring resourece.load which is very fast.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Resources.Load cannot be used with assets downloaded in a built version of the game. You will need a completely different alternative. (You should avoid using Resources.Load for anything these days, even for files that exist at build time — Unity has much better options now) Try editing your question to show us what kinds of files you're loading and how they're being downloaded, and we can suggest suitable methods. \$\endgroup\$
    – DMGregory
    Jun 2 at 11:58
  • \$\begingroup\$ @DMGregory Thank i have updated my question. \$\endgroup\$ Jun 2 at 12:03
  • \$\begingroup\$ "which is not async" so write an asynchronous version, naturally? How have you attempted this so far and where have you run into difficulty that you need help solving? \$\endgroup\$
    – DMGregory
    Jun 2 at 14:17

1 Answer 1

2
\$\begingroup\$

When you import a 3D model into your project in the Unity Editor, Unity converts the 3D model into an internal mesh format that's optimized specifically for the Unity engine (and stores this somewhere in the Library folder inside your project directory).

You noticed that if you download a file into the Resources folder in the Editor while the game is playing, you can't immediately access it using Resources.Load<>. That's because the Editor hasn't yet parsed the model file into a mesh. Calling AssetDatabase.Refresh tells the Editor to look for new files in the Resources folder and convert them into a Unity-friendly format (which is then stored somewhere in the Library folder).

When you call Resources.Load<>, Unity is not reading the original OBJ file and converting it into a mesh; it is loading the previously converted mesh. When you make a build, Unity only includes the converted mesh, not the original OBJ file.

All of the assets in the Resources folder are packaged into a single Resources file when you make a build. In a build, the Resources folder no longer exists - there is only the Resources file that contains the assets that were already converted into Unity-friendly formats by the Editor. There's no way to add new files to the Resources folder in a build because it doesn't even exist in the build.

Resources.Load<> is not particularly fast - in fact, it's a messy and inefficient way of loading files at runtime. It just seems fast compared to a runtime file importer because it doesn't have to import or parse anything. If you're trying to benchmark it against a runtime model loader, you need to also include the amount of time that AssetDatabase.Refresh() takes, because that is what actually parses and converts the mesh data from the OBJ file.

If "Runtime Obj Importer" is not fast enough, you'll either need to modify it yourself, or find a different library that can import OBJ files at runtime.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .