Script created meshes and materials

When you create a mesh via scripting, this mesh obviously doesn’t have an asset counterpart in the project. The same goes for materials etc.
Because of that, prefabs made using these objects, doesn’t work across different scenes. They simply lose the reference, as it, probably, reference down into the very scene that the objects were created in.
They also can’t be made into AssetBundles.
I see this issue has been discussed briefly about materials, but with no real solution…

Now… how in the world can Unity do it with primitives? When you create a cube (or any other primitive), it will get a mesh and a material that too aren’t represented as actual assets in the project. But those do indeed work with both prefabs and AssetBundles.

Any solutions? How can this be? Where do Unity store the reference for meshes and materials created by primitives? We want to store our objects the same place :smile:

I’m not quite sure what you mean here. If you are creating a mesh procedurally, then presumably it can be created in each scene anyway. Are you concerned about memory getting consumed when you do this?

No, I have no concerns for memory here :slight_smile:

It’s not procedurally created geometry in that sense.
It’s simply combined geometry.
I have made some functions (scripts) that can take a collection of objects, say tons of bushes in a giant area, put them into groups of objects close to each other via a distance threshold.
It then builds a low poly version of the object by rendering it from a set of angles, putting the result on some planes, and putting the result of all bushes in a group into one big atlas texture. Then the script combines the geomtry of these clumps, and add some LOD functionality to them.
Great…
but that geometry can’t work with asset bundles, as neither the meshes nor the materials has a belonging to the assetDatabase. And they too can’t be made into prefabs, as those prefabs wouldn’t work in other scenes. And when I commit the objects to the Asset Server, my co-workers lose the reference to meshes and materials, when they open them. It’s like the objects are bound to the scene, and aren’t transfered/recognised by the Asset Server.

And here comes my question again. If I make cubes and planes (primitives), they too get meshes without asset connection, and materials ditto. But these objects do work with both prefabs and Asset Bundles. How come? What does Unity do internally when creating these objects, that we can’t do?

This needs to work in the future…
Unity can’t offer methods for creating meshes and materials, and then not make it possible to use these objects in prefabs and asset bundles. Maybe it’s just a bug, but you sound like it isn’t in the other thread about materials, Andeee. Should I submit this?

A real showstopper, this problem. Our whole game is made up of various automated optimization stuff, and auto generated LOD objects, and none of it will work with Asset bundles. Or prefabs.

If only the OBJ exporter of the wiki still worked. Then I could probably come up with some solution, by saving combined meshes into objects, import these to the asset database, and then reference these objects instead. But it generates files that won’t import. No reason given, just an exeption thingy with some error without details…
Has anyone a working version of the OBJexporter?

As for “belongings to the asset db”: in this case just keep in mind that they also can’t have any scripts attached of any kind as those don’t transport with asset bundles (asset bundles only store asset db ids so asset bundles with scripts used only work with the project they were built for)

What confuses me is the part on asset server: Asset server stores whole projects, it does not store standalone objects or asset bundles.
So how are you getting it there without it being in the appropriate project already? (to which the references to meshes etc also will point)

I’m not sure I understand. Imagine this case:

I make a fense of individual modules, then combine the meshes into one using a script.
Now this new “fense” mesh hasn’t got any asset in my project, it alone exist in the scene it is created in.
I take the fense, make it into a prefab, and save my scene. I find that this new prefab, whenever I try to use it in a new scene, will have missing mesh written in it’s mesh component of its meshfilter. It will only work in the scene in which it was created.
I try to commit my project changes to the asset server. On all other computers than my own, the object will have “missing mesh”, even in the scene it was created in.
Not good…

The same goes for materials. It’s a problem to provide the tools for generating these things, and then make them not work with prefabs and asset bundles…

Normally, if you create a mesh in editor mode and assign it as sharedMesh to a MeshFilter, this mesh is stored in the scene file, but not in your asset folder (as you already found out).

To store your mesh as an asset, you can use the AssetDatabase:
AssetDatabase.CreateAsset(meshToSave, “Assets/saved_mesh.asset”);