Recording Quick Start
This guide corresponds to the video tutorial located here.
A recording consists of metadata, custom events, and subjects. The easiest way to build a recording is to make a recorder to do it for you. There are two different ways to create a recorder. The first way is through the editor by right clicking anywhere in your project and selecting Create > Record and Play > Recorder as seen in the Recorder documentation.
The other way to create a recorder is by using code like so:
var myRecorder = ScriptableObject.CreateInstance<Recorder>();
Once you have your recorder, you can register the subjects you want to be recorded. To do this you need to attatch a SubjectBehavior script to what ever GameObject you want to keep up with. There are helper functions for setting up this script called Build().
Recorder recorder = ScriptableObject.CreateInstance<Recorder>();
GameObject objectToTrack = GameObject.CreatePrimitive(PrimitiveType.Cube);
SubjectBehavior.Build(objectToTrack, recorder);
Calling Build attaches the script to what ever GameObject you passed in and registers it with the recorder passed in. For details on what framerate and minimumDelta is, please refer to SubjectRecorder. The alternative way to create a SubjectBehavior is to manually attach an instance of it to whatever GameObject you want to track and drag in a recorder for it to use in the inspector. To actually begin a recording, you must start the recorder by calling Start. While the recorder is capturing events, you can log recording custom events or subject custom events. Then to finally create a recording you need to call Finish, which will then pop you out a Recording.
Recorder recorder = ScriptableObject.CreateInstance<Recorder>();
GameObject objectToTrack = GameObject.CreatePrimitive(PrimitiveType.Cube);
SubjectBehavior subject = SubjectBehavior.Build(objectToTrack, recorder);
recorder.Start()
// ...stuff happens....
subject.CaptureCustomEvent("custom subject event", "event details");
recorder.CaptureCustomEvent("custom recorder event", "other event details");
// Finish up the recording.
Recording myRecording = recorder.Finish();
// Now you can do something with the recoding such as save it or play it back
myRecording.SaveToAssets("Some name for your asset")
PlaybackBehavior.Build(myRecording).Play();
Example
using UnityEngine;
// Import the namespace that contains the Recorder and SubjectBehavior Class.
using RecordAndPlay.Record;
///<summary>
/// This script is meant to act as a demo on creating recordings. It creates 3
/// boxes and attaches subject behavior scripts to them. It then starts the
/// recorder. The user can choose to stop the recorder by clicking the save
/// button. Clicking the save button takes the recording and saves it to assets
/// folder in the project.
///</summary>
public class RecordingExample : MonoBehaviour
{
// The recorder in charge of keeping up with all the subjects in the
//scene.
Recorder recorder;
// Start is called before the first frame update
void Start()
{
// Because the recorder is a scriptable object, we have to create
// it using CreateInstance().
recorder = ScriptableObject.CreateInstance<Recorder>();
int numberOfSubjects = 3;
for (int i = 0; i < numberOfSubjects; i++)
{
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent<Rigidbody>();
cube.transform.position = new Vector3((i \* 2) - 1.5f, 3, 0);
// Create a SubjectBehavior instance and attatch it to the
// cube. Doing this automatically registers the subject
// with the recorder passed in.
SubjectBehavior.Build(cube, recorder);
}
// Start the recorder so it will capture all events occuring both
// to it and the different subjects registered.
recorder.Start();
}
private void OnGUI()
{
// If the recorder is currently recording and the player clicks
// save...
if (recorder.CurrentlyRecording() && GUILayout.Button("Save"))
{
// Create a recording with all captured events up to this
// point and stop the recorder from accepting any new
// event captures.
var recording = recorder.Finish();
// Take the recording we just created and save it to the
// assets folder inside our project.
recording.SaveToAssets("Recording Example");
}
}
}