UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Physics.BakeMesh
# [Physics](https://docs.unity3d.com/cn/2019.4/ScriptReference/Physics.html).BakeMesh public static void **BakeMesh** (int **meshID**, bool **convex**); ## 参数 | meshID | 将从中烘焙碰撞数据的网格的实例 ID。 | | -------- | ------------------------------------- | | convex | 指示是否烘焙凸面体几何体的标志。 | ## 描述 准备要与 MeshCollider 配合使用的网格。 此函数运行网格烘焙,并将结果保存在网格本身中。当实例化引用网格的 MeshCollider 时,它重新使用已烘焙的数据,而不是重新烘焙网格。要将高成本的烘焙过程从场景加载时间或实例化时间转移到其他时间时,这非常有用。BakeMesh 具有线程安全性,并且在调用它的线程上进行计算。可将 BakeMesh 与 C# 作业系统结合使用。下面是一个示例: ``` 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 具有线程安全性,并且在调用它的线程上进行计算。可将 BakeMesh 与 C# 作业系统结合使用。 此示例说明如何在多个线程间烘焙网格,以便缩短在主线程上进行 MeshCollider 实例化的时间。 ``` 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]; } } } ```
da
2022年6月14日 15:49
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码