Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created December 16, 2022 14:35
Show Gist options
  • Save unitycoder/59ed91a8702136c64594ac2b9509f07e to your computer and use it in GitHub Desktop.
Save unitycoder/59ed91a8702136c64594ac2b9509f07e to your computer and use it in GitHub Desktop.
unity simple job system for handling thousands of moving transforms
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
public struct TransformData
{
public float3 position;
public quaternion rotation;
}
public struct UpdateTransformsJob : IJobParallelFor
{
public NativeArray<TransformData> transformData;
public void Execute(int index)
{
// Update the transform's position and rotation based on the data in the struct.
transformData[index] = new TransformData
{
position = transformData[index].position + new float3(1, 0, 0),
rotation = transformData[index].rotation * quaternion.Euler(0, 1, 0),
};
}
}
public class TransformUpdater : MonoBehaviour
{
public Transform[] transforms;
private NativeArray<TransformData> transformData;
private void Start()
{
// Create a NativeArray to store the data for all of the transforms.
transformData = new NativeArray<TransformData>(transforms.Length, Allocator.Persistent);
// Fill the NativeArray with the data for each transform.
for (int i = 0; i < transforms.Length; i++)
{
transformData[i] = new TransformData
{
position = transforms[i].position,
rotation = transforms[i].rotation,
};
}
}
private void Update()
{
// Schedule the job to run on multiple threads.
UpdateTransformsJob job = new UpdateTransformsJob
{
transformData = transformData,
};
JobHandle jobHandle = job.Schedule(transformData.Length, 64);
// Wait for the job to complete.
jobHandle.Complete();
// Update the transforms with the new position and rotation data.
for (int i = 0; i < transforms.Length; i++)
{
transforms[i].SetPositionAndRotation(transformData[i].position, transformData[i].rotation);
}
// Dispose of the NativeArray when it is no longer needed.
transformData.Dispose();
}
}

unity simple job system for handling thousands of moving transforms

// ChatGPT:

The Unity Job System is a way to run certain tasks in parallel on multiple threads. It can be a good way to improve performance in your game by offloading some work to additional threads, rather than doing everything on the main thread. Here's a high-level overview of how you could use the Unity Job System to handle thousands of moving transforms:

Create a struct to represent the data for each transform you want to update. This struct should contain the transform's position, rotation, and any other relevant data.

Create a job that processes a range of these structs and updates the transform's position and rotation based on the data in the struct.

Create a NativeArray to store the data for all of the transforms you want to update.

Use the Unity Job System's Schedule method to schedule the job to run on multiple threads, passing in the NativeArray as an argument.

When the job is complete, use the Transform.SetPositionAndRotation method to update the transform's position and rotation with the new values from the struct.

Here's some example code that demonstrates this process:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment