Created
November 20, 2023 13:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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