docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    About the ProBuilder Scripting API

    ProBuilder provides a Scripting API for C#, which you can use to extend the ProBuilder tools and windows. It includes these namespaces:

    • UnityEditor.ProBuilder provides classes and enums for Unity editor integration. Use them to extend ProBuilder menus, windows, toolbars, and Mesh operations that are only available through the ProBuilder windows and tools.
    • UnityEngine.ProBuilder provides classes, structs, and enums for compiling Meshes. Use them to access a lot of core ProBuilder functionality, such as creating Meshes, dealing with events, and some math functions.
    • UnityEngine.ProBuilder.MeshOperations provides classes for Mesh editing. Use them to manipulate ProBuilder Meshes, including topology and I/0 operations.

    All Mesh creation and editing functionality is restricted to the UnityEngine.ProBuilder and UnityEngine.ProBuilder.MeshOperations libraries, which are both available at run time.

    ProBuilder stores Mesh data in a component (ProBuilderMesh) and compiles it to a UnityEngine.Mesh object as necessary.

    ProBuilderMesh stores the following Mesh information:

    • Positions

    • UVs

    • Faces

      • Triangles

      • Material

      • Smoothing group

      • Auto/Manual UVs

        Note: ProBuilder can automatically UV unwrap triangles on a per-face basis. You can toggle this feature with the Face class. In addition, users can unwrap faces manually.

    • Tangent (if user set)

    • UV3/4 (if user set)

    • Colors

    • Shared indices (also called common vertices)

    Normals, tangents, collisions, and UVs are calculated as necessary.

    Create a Mesh

    This example demonstrates how to build a simple quad with the ProBuilder API (not with the ShapeGenerator class):

    // Create a new quad facing forward.
    ProBuilderMesh quad = ProBuilderMesh.Create(
        new Vector3[] {
            new Vector3(0f, 0f, 0f),
            new Vector3(1f, 0f, 0f),
            new Vector3(0f, 1f, 0f),
            new Vector3(1f, 1f, 0f)
        },
    	new Face[] { new Face(new int[] { 0, 1, 2, 1, 3, 2 } )
    } );
    

    Modify a Mesh

    Modifying a ProBuilder Mesh is different from modifying a Unity Mesh: instead of working with MeshFilter.sharedMesh you work with the ProBuilder representation of the Mesh: ProBuilderMesh.

    The basics are the same: set vertex positions, modify triangles (faces in ProBuilder), then rebuild the mesh. For example, to move the vertices up on that quad from the previous example:

    using UnityEngine;
    using UnityEngine.ProBuilder;
    #if UNITY_EDITOR
    using UnityEditor.ProBuilder;
    #endif
    
    public class MoveVertices : MonoBehaviour
    {
        void Start()
        {
            var cube = ShapeGenerator.CreateShape(ShapeType.Cube);
            
            // Move one face on the cube along the direction of its normal
            Vertex[] vertices = cube.GetVertices();
            Face face = cube.faces[0];
    
            Vector3 normal = Math.Normal(cube, face);
            
            // A face is a collection of triangles, stored in the indexes array. Because mesh geometry requires that seams
            // be inserted at points where normals, UVs, or other vertex attributes differ we use GetCoincidentVertices to
            // collect all vertices at a common position.
            // To see the difference, try replacing `cube.GetCoincidentVertices` with just `face.dinstinctIndexes`.
            foreach(var index in cube.GetCoincidentVertices(face.indexes))
                vertices[index].position += normal;
    
            cube.SetVertices(vertices);
            
            // Rebuild the triangle and submesh arrays, and apply vertex positions and submeshes to `MeshFilter.sharedMesh`.
            cube.ToMesh();
    
            // Recalculate UVs, Normals, Tangents, Collisions, then apply to Unity Mesh.
            cube.Refresh();
    
            // If in Editor, generate UV2 and collapse duplicate vertices.
            #if UNITY_EDITOR
            EditorMeshUtility.Optimize(cube, true);
            #else
            // At runtime, `EditorMeshUtility` is not available. To collapse duplicate
            // vertices in runtime, modify the MeshFilter.sharedMesh directly.
            // Note that any subsequent changes to `quad` will overwrite the sharedMesh.
            var umesh = cube.GetComponent<MeshFilter>().sharedMesh;
            MeshUtility.CollapseSharedVertices(umesh);    
            #endif
        }
    }
    

    Note that you should never directly modify the MeshFilter.sharedMesh. ProBuilder controls updating the Unity Mesh with ProBuilderMesh::ToMesh and ProBuilderMesh::Refresh functions.


    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Create a Mesh
    • Modify a Mesh
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)