Search Unity

Probuilder convert to mesh

Discussion in 'World Building' started by JDaze, Dec 23, 2020.

  1. JDaze

    JDaze

    Joined:
    Sep 19, 2018
    Posts:
    3
    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.
     
  2. JDaze

    JDaze

    Joined:
    Sep 19, 2018
    Posts:
    3
    Found it! In the secret menu Tools > Probuilder > Action > Strip...
     
    Aldeminor and cengage-jsal like this.
  3. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    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).
     
  4. minoaimino

    minoaimino

    Joined:
    Feb 18, 2015
    Posts:
    3
    Are there any way to convert from ProBuilder mesh to mesh(UnityEngine.Mesh) which can be used in script?
     
    Last edited: May 19, 2021
  5. minoaimino

    minoaimino

    Joined:
    Feb 18, 2015
    Posts:
    3
    I've got solved.

    Code (CSharp):
    1.                 var mesh = new Mesh();
    2.                 mesh.SetVertices(pbMesh.GetVertices().Select(o => new Vector3()
    3.                 {
    4.                     x = o.position.x,
    5.                     y = o.position.y,
    6.                     z = o.position.z,
    7.                 }).ToList());
    8.                 var indices = new List<int>();
    9.                 pbMesh.faces
    10.                     .ToList()
    11.                     .ForEach(o =>
    12.                     {
    13.                         indices.AddRange(o.indexes.ToList());
    14.                     });
    15.                 mesh.SetIndices(indices, MeshTopology.Triangles, 0);
    16.                 mesh.RecalculateBounds();
    17.                 mesh.RecalculateNormals();
     
  6. fahruz

    fahruz

    Joined:
    Mar 5, 2020
    Posts:
    57
    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 ?
     
  7. cengage-jsal

    cengage-jsal

    Joined:
    May 10, 2022
    Posts:
    27
    Confirming this worked for me in case anyone else wants to know.
     
  8. Brave_park

    Brave_park

    Joined:
    Nov 4, 2020
    Posts:
    1
    thanks thanks thanks!
    unity plz update menual.
     
  9. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    96
    @kaarrrllll

    Does not work with prefabs:
    1. 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?)
    2. 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:
    1. Create a ProBuilder object
    2. Make a prefab out of it.
    3. Open the object in the dedicated prefab scene.
    4. Run the "Strip all Probuilder scripts in scene" command from the Actions menu.
    5. Change something on the object (e.g. move it away a bit from its original position to make it dirty)
    6. Press Ctrl+S
    7. Exit Prefab mode.
    8. 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:
    upload_2023-1-22_21-43-15.png

    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.
     
    Last edited: Jan 22, 2023
    kreso, uchimatausa, fnnbrr and 3 others like this.
  10. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    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.
     
    Last edited: Jul 1, 2023
  11. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    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."

     
    Yany and SteveKouts like this.
  12. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    96
    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?
     
    uchimatausa likes this.
  13. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    The editor action "Export Prefab" is doing roughly three steps:

    1. Create a mesh asset in the project from the selected Probuilder mesh. AssetDatabase.CreateAsset(probuilderComponent.mesh, path).
    2. Remove Probuilder-specific components. See https://github.com/Unity-Technologi...tor/EditorCore/StripProBuilderScripts.cs#L138. 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.
    3. https://docs.unity3d.com/ScriptReference/PrefabUtility.SaveAsPrefabAssetAndConnect.html to replace the target GameObject.

    You can reference the source here, https://github.com/Unity-Technologi...nuActions/Export/ExportAsset.cs#L183C6-L183C6. 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.
     
    Yany likes this.
  14. TheSheyper

    TheSheyper

    Joined:
    Jul 3, 2013
    Posts:
    22
    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!