Skip to content

CyberAgentGameEntertainment/InstantReplay

Repository files navigation

Instant Replay for Unity

日本語

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.

For Sharing Gameplay on SNS

You can implement a feature that allows users to share their recent gameplay footage on social media.

For Recording Reproduction Steps of Bugs

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.

Table of Contents

Requirements

  • Unity 2022.3 or later

Supported Platforms

  • iOS
    • 13.0 or later
  • Android
  • macOS (Editor and Standalone)
    • 10.15 (Catalina) or later
  • Windows (Editor and Standalone)
  • Any other systems with ffmpeg command line tool installed in PATH

Installation

Install Dependencies

There are two ways to install the dependencies. You can use either of them.

Method 1: Install via UnityNuGet and dependency package

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#release

Method 2: Install manually

Install individual packages using NuGetForUnity or UnityNuGet:

Install the Package

Add the following git URL to the package manager:

https://github.com/CyberAgentGameEntertainment/InstantReplay.git?path=Packages/jp.co.cyberagent.instant-replay#release

Quick Start

Import "User Interfaces" sample from the package manager.

Image

Place InstantReplay Recorder.prefab in the scene. This prefab has RecorderInterface and PersistentRecorder component, which will automatically record the gameplay during enabled.

Image

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.

Image

Recorded video will be displayed on the screen.

image

Detailed Usage

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)));

Setting Recording Time and Frame Rate

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.

Setting the 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.

Setting the Video Source

By default, InstantReplay uses ScreenCapture.CaptureScreenshotIntoRenderTexture() for recording. 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(Frame frame);

    event ProvideFrame OnFrameProvided;
}

new InstantReplaySession(900, frameProvider: new CustomFrameProvider(), disposeFrameProvider: true);

Setting the Audio Source

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);

Getting the Recording State

You can get the recording state with the InstantReplaySession.State property.

Getting the Progress of Writing

You can get the progress in the range of 0.0 to 1.0 by passing IProgress<float> to InstantReplaySession.StopAndTranscodeAsync.

About

Record the latest seconds of gameplay and export it as a video at any time.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •