Alternative to Texture2D.Apply() that doesn't require synchronizing with the render thread, avoiding stalls in the main thread
UNLICENSE License
Alternative to Texture2D.Apply() that doesn't require synchronizing with the render thread, avoiding stalls in the main thread.
Either:
openupm add com.gilzoide.texture-apply-async
https://github.com/gilzoide/unity-texture-apply-async.git#1.0.0
Assets
or Packages
folder.using Gilzoide.TextureApplyAsync;
// 1. Create a `TextureApplyAsyncHandle` for your texture.
var textureApplyAsyncHandle = new TextureApplyAsyncHandle(myTexture);
// 2. If you want to update your texture every frame,
// schedule the apply handle to update every frame.
textureApplyAsyncHandle.ScheduleUpdateEveryFrame();
// 3. Update the texture data normally.
for (int x = 0; x < myTexture.width; x++)
{
for (int y = 0; y < myTexture.height; y++)
{
myTexture.SetPixel(x, y, Random.ColorHSV());
}
}
// 4. If you want to update your texture only once,
// schedule a one-shot update.
textureApplyAsyncHandle.ScheduleUpdateOnce();
// 5. Cancel updates if necessary.
// Works for both one-shot and every frame updates.
textureApplyAsyncHandle.CancelUpdates();
// 6. Dispose of the `TextureApplyAsyncHandle` when not needed
// anymore, e.g. inside a component's `OnDestroy`.
textureApplyAsyncHandle.Dispose();