This wasn't made with the gif tool... I just wanted to throw another image in here and this one's fun.

Displaying the Screenshots

When the game is paused, the pause menu has an area dedicated to displaying the replay along with sliders that allow you to set the start and end points of the loop you'd like to save to a gif. At the moment for simplicity I always record the screenshots at the same resolution regardless of the aspect ratio, but here I scale the image UI to make it appear to match the aspect ratio of the window. The images cycle along at the recorded framerate and loop correctly using the current start and end positions of the circular buffer of render textures and the user set start and end loop points. The GUI for all this took a few hours to setup all together.

Creating the GIF

One thing I didn't want to have to do was write my own GIF encoding. Typically I prefer to write my own implementations of things like this rather than use a third party library, but I wanted to complete this task over a weekend. I looked at a few C# animated gif libraries hoping to find one that would be fairly simple. NGif was the best I found for my purposes. Besides a couple of files that handled some image analysis and encoding, there was really only one file I needed to deal with. I converted its implementation from using Windows based Image and Bitmap classes to Unity textures, made some optimisations and had it working pretty quickly. At gif creation time for each of the desired frames I create actual readable textures from the render textures and perform bilinear filtered scaling to get them to the correction aspect ratio if required. These textures are then processed one at a time and encoded as gif frames. Once complete, I save the result as a gif file on the users machine in their save game directory and then provide the option to upload the gif.

Ultimately the process of finding the gif library, getting it working in Unity and setting it up for my purpose of pumping out gif files from the replay recordings took 2-3 hours.

gfycat Upload

The process of creating a gif file now was infinitely simpler than it was before, but I also wanted to remove any barrier between that step and users sharing the gif with others. I looked at the APIs for Imgur and gfycat with the intention of supporting uploading to both. Imgur's API is more mature, but there are more hoops to jump through. It also appears as though commercial applications may need to pay a fee to use the API. gfycat on the other hand has a very simple API and less restrictions. Their documentation is pretty poor with spelling and even mistakes in their URLs, but I was able to figure out the correct steps to get something uploaded and transcoded and get the result gif URL back.

Uploading to the Amazon S3 servers gfycat uses was quite slow so I made sure you could resume playing the game while it was happening. Once the upload completes, the gfycat transcode URL is called to take the uploaded gif and turn it into whatever gfycats HTML5 video stuff is. When that process completes, the URL of the result can be grabbed and at that point I display a button to open the gfycat page in a browser (user's default browser if non-Steam and Steam browser in in Steam) so you can see the result and easily copy the URL to share it. gfycat also makes the original gif file available.

Here's the gfycat I uploaded in the video above: