Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using Unity.Collections;
- using Unity.Jobs;
- using UnityEngine;
- using System.Linq;
- using Unity.Burst;
- [BurstCompile]
- 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 static class JobifiedBaking
- {
- const int meshesPerJob = 10;
- public static void BakeMeshes(Dictionary<MeshChunk,Mesh> meshes)
- {
- NativeArray<int> meshIds = new NativeArray<int>(meshes.Count, Allocator.TempJob);
- for (int i = 0; i < meshes.Keys.Count; ++i) {
- meshIds[i] = meshes[meshes.Keys.ElementAt(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.Keys.Count; i++) {
- meshes.Keys.ElementAt(i).meshCollider.sharedMesh = meshes[meshes.Keys.ElementAt(i)];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement