Is it possible to load meshes in runtime, applying some components to them?

Hello,
probably the title is too short to give you a clear idea of what I want to create.

Basically I want to be able to load from a folder (or web location) meshes, textures or other things like audio or video, after the game was succesfully compiled. With particular attention to meshes, I would like that those meshes will have some components attached in automatic to them, once they are loaded in the game.

I’ll give you a practical example of what I just said.

I have an empty box in my game, that could be filled (after a click on a button, or in automatic, doesn’t matter I guess) with meshes imported from a folder (the game at this stage, I repeat, is already compiled). I create a simple mesh called “sphere.fbx”, and I save it into the folder that should be linked to the game. If I click the button, right now, that mesh should appear inside the box.
But that’s not all yet. I need to understand if that object is, for example, a sphere, a cube, or a cone (checking the filename, or a tag, no idea how it’s possible to achieve this), and then, apply in automatic the right component (like a mesh collider, for example, or a script created by me, or just a material… like for example, a yellow material for the sphere, a purple material for the cube… etc…).

That’s it.

Now, I basically know how to do this stuff, the problem is that I’ve to do everything in runtime… and I probably have to use the “AssetBundle” stuff (I guess) which I have no idea how to use it.

Oh, and I have to do this in Virtual Reality… I don’t know if this will change something… but at least my application will run just on PC and not on mobile.

Not yet.

Hope that someone will help me…

Thank you!

some ideas,
AssetBundles is one way, but you need to create them inside unity editor…
Then there are .obj and other model format importers in asset store, that can directly load models locally or from web.

Adding components automatically,
with asset bundles:

  • you could add the components/materials already when creating the bundles (so they would be ready gameobjects to use)
    with “raw” models:
  • you could name the models like, sphere_mycolor_collidertype.fbx and use that… (or could supply some metadata file next to it, sphere.txt, and have any details there for color, collider etc…

other alternatives,
custom scripting like MoonSharp and MiniScript
you could load external scripts and basically mod your game…(to generate objects, set colors etc)

in pc platform, you can also include scripts in the assetbundles i’ve heard…(never tested)

Thank you mgear,
apparently is cool what you can do with AssetBundles, but the problem is that I can’t find enough documentation to understand how actually to implement them for the stuff I want to create in my game.

I tried to follow this guide tutorial: https://unity3d.com/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager?playlist=17117

In the editor it worked just fine, but once the game has been built, not only it doesn’t work, but I don’t have any possibility to add extra meshes in a second moment, so I don’t really have a good example to do this like I want.

Do you know any good tutorial I can follow?

Thanks.

the main problem there is that assetbundle must be generated with unity editor,
so if you want to throw in teapot.fbx, you need to convert it into asset bundle first…

not loading in build, are there any errors in player.log?

Yes! And that’s a problem.

I spent several hours to study assetbundles yesterday, and yeah.
Basically it’s a good way to save size on your project, but you have to prepare all the meshes in the editor, and that’s not useful for what I want to create.

So at this stage:

  • Resources system it’s useless.
  • AssetBundles same thing.

Could the “StreamingAssets” method be a good way to do that?
Apparently I could import anything in runtime… and maybe after that I could do the rest with a script, playing with the filename?

Let’s see if whatever I just said it’s possible with Unity… there should be a way to do this in Unity.

yeah, i’d use one of those .obj/.fbx importers in asset store and have special filename or other metadata to do whats needed.

Nothing.
Apparently it’s not possible to do my project with Unity, without to buy an FBX Importer from the Asset Store.

So sad… :frowning: