Tanoda
PlaybackRecorder.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10#if UNITY_EDITOR
11using UnityEditor;
12#endif
13using System;
14using System.IO;
15
16namespace Leap.Unity.Playback {
17
18 public class PlaybackRecorder : MonoBehaviour {
19
20 public enum RecordTime {
21 Update,
23 }
24
25 public enum SaveType {
26 None,
27 UnityAsset
28 }
29
30 [SerializeField]
32
33 [SerializeField]
34 protected RecordTime _recordTime = RecordTime.Update;
35
36 [Header("Editor Settings")]
37 [SerializeField]
38 protected KeyCode _startRecording = KeyCode.F5;
39
40 [SerializeField]
41 protected KeyCode _endRecording = KeyCode.F6;
42
43 [SerializeField]
44 protected SaveType _saveType = SaveType.None;
45
46 [SerializeField]
47 protected string _unityAssetSavePath = "Assets/Recording";
48
49 protected float _beginTime;
51
52 public virtual void StartRecording() {
53 switch (_recordTime) {
54 case RecordTime.FixedUpdate:
55 _beginTime = Time.fixedTime;
56 break;
57 case RecordTime.Update:
58 _beginTime = Time.time;
59 break;
60 }
61 _currentRecording = ScriptableObject.CreateInstance<Recording>();
62 }
63
64 public virtual Recording EndRecording() {
65 Recording finishedRecording = _currentRecording;
66 _currentRecording = null;
67
68 switch (_saveType) {
69 case SaveType.None:
70 break;
71 case SaveType.UnityAsset:
72#if UNITY_EDITOR
73 Directory.CreateDirectory(_unityAssetSavePath + ".dummy");
74 string path = AssetDatabase.GenerateUniqueAssetPath(_unityAssetSavePath + ".asset");
75 AssetDatabase.CreateAsset(finishedRecording, path);
76 AssetDatabase.SaveAssets();
77 break;
78#else
79 throw new Exception("Cannot save unity assets outside of Unity Editor");
80#endif
81 default:
82 break;
83 }
84
85 return finishedRecording;
86 }
87
88 protected virtual void Update() {
89 if (_currentRecording != null) {
90 if (_recordTime == RecordTime.Update) {
92 if (frame != null) {
93 _currentRecording.frames.Add(new Frame().CopyFrom(frame));
95 }
96 }
97
98 if (Input.GetKeyDown(_endRecording)) {
100 }
101 } else {
102 if (Input.GetKeyDown(_startRecording)) {
104 }
105 }
106 }
107
108 protected virtual void FixedUpdate() {
109 if (_currentRecording != null && _recordTime == RecordTime.FixedUpdate) {
111 if (frame != null) {
112 _currentRecording.frames.Add(new Frame().CopyFrom(frame));
113 _currentRecording.frameTimes.Add(Time.fixedTime - _beginTime);
114 }
115 }
116 }
117 }
118}
The Frame class represents a set of hand and finger tracking data detected in a single frame.
Definition: Frame.cs:24
Provides Frame object data to the Unity application by firing events as soon as Frame data is availab...
Definition: LeapProvider.cs:21
abstract Frame CurrentFrame
The current frame for this update cycle, in world space.
Definition: LeapProvider.cs:37
abstract Frame CurrentFixedFrame
The current frame for this fixed update cycle, in world space.
Definition: LeapProvider.cs:47