using System; using Unity.Burst; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using Unity.Entities; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// namespace Rukhanka.WaybackMachine { [BurstCompile] public struct WaybackMachineData: IDisposable { public enum FPSMode { FPS120, FPS60, FPS30 } public FPSMode fpsMode; public int lastRecordedFrame; public NativeList animHistory; public NativeList controllerStateHistory; public NativeList controllerTransitionHistory; public NativeList animationEventHistory; public NativeList animatorEventHistory; public NativeList emittedAnimationEvents; public NativeList emittedAnimatorEvents; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void Clear() { foreach (var ah in animHistory) { ah.Dispose(); } lastRecordedFrame = 0; animHistory.Clear(); animationEventHistory.Clear(); animatorEventHistory.Clear(); controllerStateHistory.Clear(); controllerTransitionHistory.Clear(); EndFrame(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void EndFrame() { emittedAnimationEvents.Clear(); emittedAnimatorEvents.Clear(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void Construct() { fpsMode = FPSMode.FPS60; animHistory = new (0xff, Allocator.Persistent); animationEventHistory = new (0xff, Allocator.Persistent); animatorEventHistory = new (0xff, Allocator.Persistent); controllerStateHistory = new (0xff, Allocator.Persistent); controllerTransitionHistory = new (0xff, Allocator.Persistent); emittedAnimationEvents = new (0xff, Allocator.Persistent); emittedAnimatorEvents = new (0xff, Allocator.Persistent); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public float GetFrameDuration() => fpsMode switch { FPSMode.FPS30 => 1 / 30.0f, FPSMode.FPS60 => 1 / 60.0f, FPSMode.FPS120 => 1 / 120.0f, _ => 1 / 60.0f }; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public long GetDataSize() { var rv = 0L; rv += animHistory.Length * UnsafeUtility.SizeOf(); foreach (var ah in animHistory) { rv += ah.historyWeights.Length * UnsafeUtility.SizeOf(); rv += ah.historyAnimTime.Length * UnsafeUtility.SizeOf(); } rv += controllerStateHistory.Length * UnsafeUtility.SizeOf(); rv += controllerTransitionHistory.Length * UnsafeUtility.SizeOf(); rv += animationEventHistory.Length * UnsafeUtility.SizeOf(); rv += animatorEventHistory.Length * UnsafeUtility.SizeOf(); return rv; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void Dispose() { foreach (var ah in animHistory) { ah.Dispose(); } animHistory.Dispose(); animationEventHistory.Dispose(); animatorEventHistory.Dispose(); controllerStateHistory.Dispose(); controllerTransitionHistory.Dispose(); emittedAnimationEvents.Dispose(); emittedAnimatorEvents.Dispose(); } } //---------------------------------------------------------------------------------------// public struct RecordComponent: IComponentData { public NativeReference wbData; } //---------------------------------------------------------------------------------------// public struct PlaybackComponent: IComponentData { public NativeList playbackData; } }