ManagedMotionData.cs
Runtime/Core/ManagedMotionData.cs
1 // Unity 6.7 reports DEVELOPMENT_BUILD even in a branch it does not compile. Earlier versions
2 // have no other symbol for a development build, so only this guard is exempt.
3 #pragma warning disable UAC0009
4 #if UNITY_7000_0_OR_NEWER
5 #if UNITY_ENABLE_CHECKS || UNITY_EDITOR
6 #define MEATMOTION_DEBUG_NAMES
7 #endif
8 #elif DEVELOPMENT_BUILD || UNITY_EDITOR
9 #define MEATMOTION_DEBUG_NAMES
10 #endif
11 #pragma warning restore UAC0009
12 using System;
13 using System.Runtime.InteropServices;
14 using Unity.Collections.LowLevel.Unsafe;
15
16 namespace MeatMotion
17 {
18 [StructLayout(LayoutKind.Auto)]
19 public struct ManagedMotionData
20 {
21 public bool CancelOnError, SkipValuesDuringDelay;
22 public byte StateCount;
23 public object State0, State1, State2, UpdateAction;
24 public Action<int> OnLoopCompleteAction;
25 public Action OnCompleteAction, OnCancelAction;
26 #if MEATMOTION_DEBUG_NAMES
27 public string DebugName;
28 #endif
29
30 // The binder guarantees that the delegate argument types match the supplied states.
31 public void UpdateUnsafe<TValue>(in TValue value) where TValue : unmanaged
32 {
33 if (UpdateAction == null) return;
34 switch (StateCount)
35 {
36 case 0: ((Action<TValue>)UpdateAction)(value); break;
37 case 1: UnsafeUtility.As<object, Action<TValue, object>>(ref UpdateAction)(value, State0); break;
38 case 2: UnsafeUtility.As<object, Action<TValue, object, object>>(ref UpdateAction)(value, State0, State1); break;
39 case 3: UnsafeUtility.As<object, Action<TValue, object, object, object>>(ref UpdateAction)(value, State0, State1, State2); break;
40 }
41 }
42
43 internal static void Invoke(Action callback)
44 {
45 try { callback?.Invoke(); }
46 catch (Exception error) { MotionDispatcher.Report(error); }
47 }
48
49 public readonly void InvokeOnCancel() => Invoke(OnCancelAction);
50 public readonly void InvokeOnComplete() => Invoke(OnCompleteAction);
51 public readonly void InvokeOnLoopComplete(int completedLoops)
52 {
53 try { OnLoopCompleteAction?.Invoke(completedLoops); }
54 catch (Exception error) { MotionDispatcher.Report(error); }
55 }
56 }
57 }