meshID | The instance ID of the Mesh to bake collision data from. |
convex | A flag to indicate whether to bake convex geometry or not. |
Prepares the Mesh for use with a MeshCollider.
This function runs Mesh baking and saves the result in the Mesh itself. When a MeshCollider referencing this Mesh is instantiated, it reuses the baked data instead of re-baking the Mesh. This is useful when you want to move the expensive baking process from the Scene load time or instantiation time to a different moment in time. BakeMesh is thread-safe, and does computations on the thread it was called from, however it is not valid to call BakeMesh on the same mesh from multiple threads at the same time as that causes undefined behaviour. You can use BakeMesh with the C# job system. Here is an example:
using UnityEngine;
public class MinimalTest : MonoBehaviour { public Mesh mesh;
private MeshCollider collider;
private void OnEnable() { // Bake this Mesh to use later. Physics.BakeMesh(mesh.GetInstanceID(), false); }
public void FixedUpdate() { // If the collider wasn't yet created - create it now. if (collider == null) { // No mesh baking will happen here because the mesh was pre-baked, making instantiation faster. collider = new GameObject().AddComponent<MeshCollider>(); collider.sharedMesh = mesh; } } }
BakeMesh is thread-safe, and does computations on the thread it was called from. You can use BakeMesh with the C# job system. This example shows how to bake meshes across multiple threads so that MeshCollider instantiation takes less time on the main thread.
using Unity.Collections; using Unity.Jobs; using UnityEngine;
public struct BakeJob : IJobParallelFor { private NativeArray<int> meshIds;
public BakeJob(NativeArray<int> meshIds) { this.meshIds = meshIds; }
public void Execute(int index) { Physics.BakeMesh(meshIds[index], false); } }
public class JobifiedBaking : MonoBehaviour { public Mesh[] meshes; public int meshesPerJob = 10;
// Bake all the Meshes off of the main thread, and then instantiate on the main thread. private void OnEnable() { // You cannot access GameObjects and Components from other threads directly. // As such, you need to create a native array of instance IDs that BakeMesh will accept. NativeArray<int> meshIds = new NativeArray<int>(meshes.Length, Allocator.TempJob);
for (int i = 0; i < meshes.Length; ++i) { meshIds[i] = meshes[i].GetInstanceID(); }
// This spreads the expensive operation over all cores. var job = new BakeJob(meshIds); job.Schedule(meshIds.Length, meshesPerJob).Complete();
meshIds.Dispose();
// Now instantiate colliders on the main thread. for (int i = 0; i < meshes.Length; ++i) { new GameObject().AddComponent<MeshCollider>().sharedMesh = meshes[i]; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
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.