How do I convert the probuilder object to a mesh? (The opposite of ProBuilderize.)
I don’t want to export to hundreds of objects.
What I actually want is to create an assetbundle that doesn’t contain probuilder dependencies.
Found it! In the secret menu Tools > Probuilder > Action > Strip…
You can also use the “Export” action if you want to save your meshes to the Project directory. Use “Export Asset” or “Export Prefab” (the former only exports a mesh asset, while the latter saves the entire GameObject + Mesh).
Are there any way to convert from ProBuilder mesh to mesh(UnityEngine.Mesh) which can be used in script?
I’ve got solved.
var mesh = new Mesh();
mesh.SetVertices(pbMesh.GetVertices().Select(o => new Vector3()
{
x = o.position.x,
y = o.position.y,
z = o.position.z,
}).ToList());
var indices = new List<int>();
pbMesh.faces
.ToList()
.ForEach(o =>
{
indices.AddRange(o.indexes.ToList());
});
mesh.SetIndices(indices, MeshTopology.Triangles, 0);
mesh.RecalculateBounds();
mesh.RecalculateNormals();
I want to do the same thing, but I also want the uv maps. Your code doesn’t pass on the uv maps, does it @minoaimino ?
Confirming this worked for me in case anyone else wants to know.
thanks thanks thanks!
unity plz update menual.
Does not work with prefabs:
- When I run the “Strip…” command from the menu, it removes the script and the object seems to be fine, but when I exit prefab mode back to my scene and reopen the prefab, the changes are not saved. (Dirty flag is not set?)
- If I change some other properties on the object after the strip is done, and I save the changes then the prefab is modified, so the script is actually stripped, but the mesh has gone. So it’s a completely destructive operation for prefabs, sounds rather serious to fix it very soon.
Unity v2022.2.2f1.
Steps:
- Create a ProBuilder object
- Make a prefab out of it.
- Open the object in the dedicated prefab scene.
- Run the “Strip all Probuilder scripts in scene” command from the Actions menu.
- Change something on the object (e.g. move it away a bit from its original position to make it dirty)
- Press Ctrl+S
- Exit Prefab mode.
- Reopen the prefab in prefab mode.
Boom, the mesh has gone.
No Probuilder, no standard mesh, just a “[none] (MeshFilter)” caption on the inspector panel:
Edit: This feature is dangerous even with scene objects as the resulting object after a strip works though, but if you just drag and drop it to make a prefab out of it, it loses the mesh without any warnings. Basically breaks the standard Unity workflow when operating with prefabs in a data-destructing way.
This is a fairly common problem in Unity. You need to save the mesh as an asset somewhere. I looking around now to see what the nicest current solution is. I think this looks good https://github.com/pharan/Unity-MeshSaver
[edit] yes, it works in 2022.2 and it was written in 2018 so it should be backward compatible. You will need to save mesh as anew instance I think.
That’s correct, the strip action only removes the Probuilder component. It does not export a mesh asset outside of the scene. That is why I recommended using “Export Asset” or “Export Prefab.”
I have a bunch of generated prefabs using ProBuilder. How can I convert them to prefabs that use regular mesh filter and mesh renderer components from script?
In the source code I can see that neither the ExportAsset.ExportMesh
is not accessible, neither the ProBuilderMesh.mesh
field. So I cannot even reproduce the functionality of the ExportMesh()
. Is it an intentional restriction? Seems very odd to me. Can anybody come up with a solution please?
The editor action “Export Prefab” is doing roughly three steps:
- Create a mesh asset in the project from the selected Probuilder mesh. AssetDatabase.CreateAsset(probuilderComponent.mesh, path).
- Remove Probuilder-specific components. See com.unity.probuilder/Editor/EditorCore/StripProBuilderScripts.cs at e36600357d36d71689c5caaa75bfb42db10540cc · Unity-Technologies/com.unity.probuilder · GitHub. There is no convenience method for this, but it boils down to setting
ProBuilderMesh.preserveMeshAssetOnDestroy
and then destroying any ProBuilder components you don’t want to be a part of the prefab. - Unity - Scripting API: PrefabUtility.SaveAsPrefabAssetAndConnect to replace the target GameObject.
You can reference the source here, com.unity.probuilder/Editor/MenuActions/Export/ExportAsset.cs at e36600357d36d71689c5caaa75bfb42db10540cc · Unity-Technologies/com.unity.probuilder · GitHub. This does a lot of extra boilerplate to handle various cases, but the core is what I’ve described above.
Alternatively, just clone the Probuilder repo, embed it in your Project/Packages directory and make whatever you need public.
Hi !
Sorry to necro,
I had more or less the same problem, I needed a copy of a probuilder mesh, I tried UnityEngine.ProBuilder.MeshUtility.Compile but strangely it generated more vertices. So I ended up just doing a basic Instantiate and it works perfectly.
mesh = Instantiate(meshFilter.sharedMesh);
@kaarrrllll do you know why MeshUtility.Compile generates more vertices ?
Thanks in advance!