- Home /
Creating new prefab at runtime
Hi, my company is developing a viewer that will download meshes from the Interwebs at runtime and populate a scene using multiple instances of those meshes. It seems to me the process flow would be something along the lines of:
- Download mesh information
- Create new Prefab object
- Parse downloaded mesh information and build a Mesh object and assign it to the newly created Prefab
- Instantiate multiple copies of the prefab and move them around the scene.
Now we've managed to do steps 1, 3, and 4. But for step 2, we're using a prefab we've previously created in the Unity editor (it's just an empty box) at compile time. Our application won't know the number of custom prefabs that will be loaded, so I don't think it makes sense to create a finite number of prefabs in the editor at compile time. Is it possible to create a new Prefab from scratch from C# scripts? I've looked around but all the resources I've found are regarding creating Prefabs in the editor at compile time. Thanks for your time and help!
Answer by Mike 3 · Jun 16, 2010 at 05:07 PM
You wouldn't be creating prefabs, you can just create new gameobjects and add the necessary components to them
example in c#:
GameObject go = new GameObject("Bob");
MeshFilter filter = go.AddComponent<MeshFilter>();
MeshRenderer renderer = go.AddComponent<MeshRenderer>();
filter.mesh = yourMesh;
You can then Instantiate that object over and over if you like, as if it were a prefab
Thanks for the quick response. I got that working fine.
We still have yet to load in our complex meshes (doing test with single triangles), but from the performance perspective, I'm assu$$anonymous$$g prefabs have some optimizations with regards to handling all the instances of it. Our application will be making hundreds, potentially thousands of copies of the same object. Will this method without prefabs still perform well, or am I just mistaken? Thanks again for the quick response.
It should work just the same - it uses the same object clone methods internally either way
Hi all...
This seems excatly what I need for a project. I have to load potentially thousands of references to .FBX files stored in X$$anonymous$$L files and use them to call the objects from the project's interface.
Users will then choose the objects they want to load and they must be dynamically loaded. Could you confirm if this could be a way of doing so?
$$anonymous$$any many thanks.
Jose
Hi all
I also want to create a prefab at runtime. Specifically, I want to make a tool for converting assets that I've built into prefabs which I can later load remotely.
What I'm actually trying to do it save constructed assets as assetBundles, which says it needs prefabs. Doing this through the editor is not an option because it needs to be completely scripted/automated.
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
create 3d model at runtime 1 Answer
assign ui panel to a prefab object ? 2 Answers
Creating and saving prefabs during runtime 0 Answers
snapping objects at runtime 2 Answers
Delete all prefabs 1 Answer