Instant Replay is a library that allows you to save recent gameplay videos at any time in Unity. You can save recent game footage retroactively when needed, ensuring you don't miss recording important moments. The recording time is limited to a pre-specified length, and frames exceeding this limit are discarded.
You can implement a feature that allows users to share their recent gameplay footage on social media.
When a bug occurs, you can export the operations performed up to that point as a video, which can be useful for reproducing the bug.
- Unity 2022.3 or later
- iOS (13.0 or later)
- Android
- macOS (Editor and Standalone)
- Windows (Editor and Standalone)
There are two ways to install the dependencies. You can use either of them.
Add UnityNuGet scoped registry and add the following git URL to the package manager:
https://github.com/CyberAgentGameEntertainment/InstantReplay.git?path=/Packages/jp.co.cyberagent.instant-replay.dependencies
Install individual packages using NuGetForUnity or UnityNuGet:
Add the following git URL to the package manager:
https://github.com/CyberAgentGameEntertainment/InstantReplay.git?path=Packages/jp.co.cyberagent.instant-replay
Import "User Interfaces" sample from the package manager.
Place InstantReplay Recorder.prefab in the scene. This prefab has RecorderInterface and PersistentRecorder component, which will automatically record the gameplay during enabled.
Then, you can stop the recording and save the video by calling RecorderInterface.StopAndTranscode(). For example, you can trigger this method by clicking the button in the scene.
Recorded video will be displayed on the screen.
To record the gameplay, use InstantReplaySession.
using InstantReplay;
var ct = destroyCancellationToken;
// Start recording
using var session = new InstantReplaySession(numFrames: 900, fixedFrameRate: 30);
// 〜 Gameplay 〜
await Task.Delay(10000, ct);
// Stop recording and transcode
var outputPath = await session.StopAndTranscodeAsync(ct: ct);
File.Move(outputPath, Path.Combine(Application.persistentDataPath, Path.GetFileName(outputPath)));You can specify numFrames and fixedFrameRate in the InstantReplaySession constructor.
new InstantReplaySession(numFrames: 900, fixedFrameRate: 30);If you set fixedFrameRate to null, the actual FPS will be used.
Frames exceeding numFrames will be discarded from the oldest. The disk usage during recording increases in proportion to numFrames, so set it to an appropriate size.
By default, it records at the actual screen size, but you can also specify maxWidth and maxHeight in the InstantReplaySession constructor. If you specify maxWidth and maxHeight, it will automatically resize. Reducing the size can reduce the disk usage and time required for writing during recording. It also reduces memory usage during recording.
By default, it supports BiRP and SRP (using RenderPipelineManager.endContextRendering and ScreenCapture.CaptureScreenshotIntoRenderTexture). You can also use any RenderTexture as the source.
Create a class that inherits InstantReplay.IFrameProvider and pass it as frameProvider to the InstantReplaySession constructor. You can also specify whether InstantReplaySession automatically discards frameProvider by disposeFrameProvider.
public interface IFrameProvider : IDisposable
{
public delegate void ProvideFrame(RenderTexture frame, double timestamp);
event ProvideFrame OnFrameProvided;
}
new InstantReplaySession(900, frameProvider: new CustomFrameProvider(), disposeFrameProvider: true);By default, it captures the audio via OnAudioFilterRead. THis automatically searches for and uses a specific AudioListener on the scene.
Warning
AudioSource with Bypass Listener Effects will not be captured.
If there are multiple AudioListeners in the scene, you can specify which one to use by passing it to the InstantReplay.UnityAudioSampleProvider constructor and then passing it as audioSampleProvider to the InstantReplaySession constructor.
new InstantReplaySession(900, audioSampleProvider: new UnityAudioSampleProvider(audioListener), disposeAudioSampleProvider: true);If you want to disable the audio, you can use NullAudioSampleProvider.Instance.
new InstantReplaySession(900, audioSampleProvider: NullAudioSampleProvider.Instance);Note
You don't have to care about IDisposable of NullAudioSampleProvider.
You can also use your own audio source by implementing IAudioSampleProvider.
public interface IAudioSampleProvider : IDisposable
{
public delegate void ProvideAudioSamples(ReadOnlySpan<float> samples, int channels, int sampleRate,
double timestamp);
event ProvideAudioSamples OnProvideAudioSamples;
}
new InstantReplaySession(900, audioSampleProvider: new CustomAudioSampleProvider(), disposeFrameProvider: true);You can get the recording state with the InstantReplaySession.State property.
You can get the progress in the range of 0.0 to 1.0 by passing IProgress<float> to InstantReplaySession.StopAndTranscodeAsync.
