Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Created November 20, 2023 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olokobayusuf/8a74a94e83ecfdaba65ac7744486c464 to your computer and use it in GitHub Desktop.
Save olokobayusuf/8a74a94e83ecfdaba65ac7744486c464 to your computer and use it in GitHub Desktop.
Creating an animated GIF image from an array of textures in Unity with VideoKit.
public static async Task<MediaAsset> CreateGIF (Texture2D[] textures) {
// Make sure `textures` is not empty
// Make sure every texture has the same size (width and height)
// Make sure every texture has the `TextureFormat.RGBA32` format
// Create recorder
var width = textures[0].width;
var height = textures[0].height;
var frameRate = 5;
var recorder = await MediaRecorder.Create(
MediaFormat.GIF,
width: width,
height: height,
frameRate: frameRate
);
// Commit your frames
foreach (var texture in textures)
recorder.CommitFrame(texture.GetRawTextureData<byte>(), 0L);
// Finish recording
var path = await recorder.FinishWriting(); // On WebGL, this `path` is a blob URL.
var asset = await MediaAsset.FromFile(path); // VideoKit offers useful utilities with the `MediaAsset` class
// Return
return asset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment