Skip to content

Commit c5ef1ce

Browse files
committed
Documentation, fixes, improvements
+Documentation update +Fixed edge case when rewinding oldest value +Properly supporting change of Time.timeScale at any point +Cleaned CircularBuffer class +Fixed unecessary memory usage even when user didnt opt to track certain attributes
1 parent 311c64b commit c5ef1ce

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed
2.18 KB
Binary file not shown.

Assets/TimeRewinder/TimeRewinderImplementation/Animations/AutoResizeAnim.anim

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ AnimationClip:
7979
- serializedVersion: 3
8080
time: 0
8181
value: 0
82-
inSlope: -1
83-
outSlope: -1
82+
inSlope: -0.99
83+
outSlope: -0.99
8484
tangentMode: 34
8585
weightedMode: 0
8686
inWeight: 0.33333334
8787
outWeight: 0.33333334
8888
- serializedVersion: 3
8989
time: 1
90-
value: -1
91-
inSlope: -1
92-
outSlope: -1
90+
value: -0.99
91+
inSlope: -0.99
92+
outSlope: -0.99
9393
tangentMode: 34
9494
weightedMode: 0
9595
inWeight: 0.33333334
@@ -284,17 +284,17 @@ AnimationClip:
284284
- serializedVersion: 3
285285
time: 0
286286
value: 0
287-
inSlope: -1
288-
outSlope: -1
287+
inSlope: -0.99
288+
outSlope: -0.99
289289
tangentMode: 34
290290
weightedMode: 0
291291
inWeight: 0.33333334
292292
outWeight: 0.33333334
293293
- serializedVersion: 3
294294
time: 1
295-
value: -1
296-
inSlope: -1
297-
outSlope: -1
295+
value: -0.99
296+
inSlope: -0.99
297+
outSlope: -0.99
298298
tangentMode: 34
299299
weightedMode: 0
300300
inWeight: 0.33333334

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/CircularBuffer.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
public class CircularBuffer <T>
45
{
@@ -14,7 +15,7 @@ public CircularBuffer()
1415
{
1516
try
1617
{
17-
howManyRecordsPerSecond = Time.timeScale / Time.fixedDeltaTime;
18+
howManyRecordsPerSecond = 1 / Time.fixedDeltaTime;
1819
bufferCapacity = (int)(RewindManager.Instance.HowManySecondsToTrack *howManyRecordsPerSecond);
1920
dataArray = new T[bufferCapacity];
2021
RewindManager.BuffersRestore += MoveLastBufferPosition;
@@ -61,30 +62,25 @@ public T ReadLastValue()
6162
/// <returns></returns>
6263
public T ReadFromBuffer(float seconds)
6364
{
64-
int howManyBeforeLast = (int)(howManyRecordsPerSecond * seconds);
65-
66-
if((bufferCurrentPosition-howManyBeforeLast) <0)
67-
{
68-
int showingIndex = bufferCapacity - (howManyBeforeLast - bufferCurrentPosition);
69-
return dataArray[showingIndex];
70-
}
71-
else
72-
{
73-
return dataArray[bufferCurrentPosition - howManyBeforeLast];
74-
}
65+
return dataArray[CalculateIndex(seconds)];
7566
}
7667
private void MoveLastBufferPosition(float seconds)
7768
{
78-
int howManyBeforeLast=Mathf.RoundToInt(howManyRecordsPerSecond*seconds);
69+
bufferCurrentPosition= CalculateIndex(seconds);
70+
}
71+
private int CalculateIndex(float seconds)
72+
{
73+
double secondsRound = Math.Round(seconds, 2);
74+
int howManyBeforeLast = (int)(howManyRecordsPerSecond * secondsRound);
7975

80-
int moveBy = - (howManyBeforeLast - bufferCurrentPosition) - 1;
76+
int moveBy = bufferCurrentPosition - howManyBeforeLast;
8177
if (moveBy < 0)
8278
{
83-
bufferCurrentPosition = bufferCapacity +moveBy;
79+
return bufferCapacity + moveBy;
8480
}
8581
else
8682
{
87-
bufferCurrentPosition -= (howManyBeforeLast+1);
88-
}
83+
return bufferCurrentPosition- howManyBeforeLast;
84+
}
8985
}
9086
}

Assets/TimeRewinder/TimeRewinderImplementation/Scripts/RewindAbstract.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ public void MainInit()
2020

2121
trackedActiveStates = new CircularBuffer<bool>();
2222
trackedTransformValues = new CircularBuffer<TransformValues>();
23-
trackedVelocities = new CircularBuffer<VelocityValues>();
24-
trackedAnimationTimes = new List<CircularBuffer<AnimationValues>>();
23+
24+
if(body!=null||body2!=null)
25+
trackedVelocities = new CircularBuffer<VelocityValues>();
26+
2527
if (animator != null)
28+
{
29+
trackedAnimationTimes = new List<CircularBuffer<AnimationValues>>();
2630
for (int i = 0; i < animator.layerCount; i++)
2731
trackedAnimationTimes.Add(new CircularBuffer<AnimationValues>());
28-
trackedAudioTimes = new CircularBuffer<AudioTrackedData>();
32+
}
33+
34+
if(audioSource!=null)
35+
trackedAudioTimes = new CircularBuffer<AudioTrackedData>();
2936
}
3037
#region ActiveState
3138
CircularBuffer<bool> trackedActiveStates;

0 commit comments

Comments
 (0)