MotionSettings.cs
Runtime/MotionSettings.cs
1 using System;
2 using UnityEngine;
3 using MeatMotion.Adapters;
4
5 namespace System.Runtime.CompilerServices
6 {
7 internal sealed class IsExternalInit { }
8 }
9
10 namespace MeatMotion
11 {
12 [Serializable]
13 public record MotionSettings<TValue, TOptions>
14 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions
15 {
16 [SerializeField] TValue startValue;
17 [SerializeField] TValue endValue;
18 [SerializeField] float duration;
19 [SerializeField] TOptions options;
20 [SerializeField] Ease ease;
21 [SerializeField] AnimationCurve customEaseCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
22 [SerializeField] float delay;
23 [SerializeField] DelayType delayType;
24 [SerializeField] int loops = 1;
25 [SerializeField] LoopType loopType;
26 [SerializeField] bool cancelOnError;
27 [SerializeField] bool skipValuesDuringDelay;
28 [SerializeField] bool immediateBind;
29 internal IMotionScheduler scheduler;
30
31 public TValue StartValue { get => startValue; init => startValue = value; }
32 public TValue EndValue { get => endValue; init => endValue = value; }
33 public float Duration { get => duration; init => duration = value; }
34 public TOptions Options { get => options; init => options = value; }
35 public Ease Ease { get => ease; init => ease = value; }
36 public AnimationCurve CustomEaseCurve { get => customEaseCurve; init => customEaseCurve = value; }
37 public float Delay { get => delay; init => delay = value; }
38 public DelayType DelayType { get => delayType; init => delayType = value; }
39 public int Loops { get => loops; init => loops = value; }
40 public LoopType LoopType { get => loopType; init => loopType = value; }
41 public bool CancelOnError { get => cancelOnError; init => cancelOnError = value; }
42 public bool SkipValuesDuringDelay { get => skipValuesDuringDelay; init => skipValuesDuringDelay = value; }
43 public bool ImmediateBind { get => immediateBind; init => immediateBind = value; }
44 public IMotionScheduler Scheduler { get => scheduler; init => scheduler = value; }
45 }
46
47 // Index is stored in Unity assets. Keep these entries in serialized order.
48 internal enum SerializedScheduler : byte
49 {
50 Default,
51 Initialization,
52 InitializationIgnoreTimeScale,
53 InitializationRealtime,
54 EarlyUpdate,
55 EarlyUpdateIgnoreTimeScale,
56 EarlyUpdateRealtime,
57 FixedUpdate,
58 PreUpdate,
59 PreUpdateIgnoreTimeScale,
60 PreUpdateRealtime,
61 Update,
62 UpdateIgnoreTimeScale,
63 UpdateRealtime,
64 PreLateUpdate,
65 PreLateUpdateIgnoreTimeScale,
66 PreLateUpdateRealtime,
67 PostLateUpdate,
68 PostLateUpdateIgnoreTimeScale,
69 PostLateUpdateRealtime,
70 TimeUpdate,
71 TimeUpdateIgnoreTimeScale,
72 TimeUpdateRealtime,
73 Manual,
74 }
75
76 #if UNITY_6000_7_OR_NEWER
77 [Unity.Scripting.LifecycleManagement.NoAutoStaticsCleanup] // Immutable.
78 #endif
79 internal static class SerializedSchedulers
80 {
81 internal static readonly IMotionScheduler[] Values =
82 {
83 null,
84 MotionScheduler.Initialization,
85 MotionScheduler.InitializationIgnoreTimeScale,
86 MotionScheduler.InitializationRealtime,
87 MotionScheduler.EarlyUpdate,
88 MotionScheduler.EarlyUpdateIgnoreTimeScale,
89 MotionScheduler.EarlyUpdateRealtime,
90 MotionScheduler.FixedUpdate,
91 MotionScheduler.PreUpdate,
92 MotionScheduler.PreUpdateIgnoreTimeScale,
93 MotionScheduler.PreUpdateRealtime,
94 MotionScheduler.Update,
95 MotionScheduler.UpdateIgnoreTimeScale,
96 MotionScheduler.UpdateRealtime,
97 MotionScheduler.PreLateUpdate,
98 MotionScheduler.PreLateUpdateIgnoreTimeScale,
99 MotionScheduler.PreLateUpdateRealtime,
100 MotionScheduler.PostLateUpdate,
101 MotionScheduler.PostLateUpdateIgnoreTimeScale,
102 MotionScheduler.PostLateUpdateRealtime,
103 MotionScheduler.TimeUpdate,
104 MotionScheduler.TimeUpdateIgnoreTimeScale,
105 MotionScheduler.TimeUpdateRealtime,
106 MotionScheduler.Manual,
107 };
108 }
109
110 [Serializable]
111 public sealed record SerializableMotionSettings<TValue, TOptions> : MotionSettings<TValue, TOptions>, ISerializationCallbackReceiver
112 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions
113 {
114 [SerializeField] SerializedScheduler schedulerType;
115 #if UNITY_EDITOR
116 #pragma warning disable 0169
117 [SerializeField] bool additionalSettings;
118 #pragma warning restore 0169
119 #endif
120 public void OnBeforeSerialize()
121 {
122 int index = Array.IndexOf(SerializedSchedulers.Values, Scheduler);
123 if (index >= 0) schedulerType = (SerializedScheduler)index;
124 }
125
126 public void OnAfterDeserialize()
127 {
128 var values = SerializedSchedulers.Values;
129 int index = (int)schedulerType;
130 scheduler = index < values.Length ? values[index] : null;
131 }
132 }
133
134 public static partial class MMotion
135 {
136 public static MotionBuilder<float, NoOptions, FloatMotionAdapter> Create(MotionSettings<float, NoOptions> settings)
137 => Create<float, NoOptions, FloatMotionAdapter>(settings);
138
139 public static MotionBuilder<double, NoOptions, DoubleMotionAdapter> Create(MotionSettings<double, NoOptions> settings)
140 => Create<double, NoOptions, DoubleMotionAdapter>(settings);
141
142 public static MotionBuilder<int, IntegerOptions, IntMotionAdapter> Create(MotionSettings<int, IntegerOptions> settings)
143 => Create<int, IntegerOptions, IntMotionAdapter>(settings);
144
145 public static MotionBuilder<long, IntegerOptions, LongMotionAdapter> Create(MotionSettings<long, IntegerOptions> settings)
146 => Create<long, IntegerOptions, LongMotionAdapter>(settings);
147
148 public static MotionBuilder<Vector2, NoOptions, Vector2MotionAdapter> Create(MotionSettings<Vector2, NoOptions> settings)
149 => Create<Vector2, NoOptions, Vector2MotionAdapter>(settings);
150
151 public static MotionBuilder<Vector3, NoOptions, Vector3MotionAdapter> Create(MotionSettings<Vector3, NoOptions> settings)
152 => Create<Vector3, NoOptions, Vector3MotionAdapter>(settings);
153
154 public static MotionBuilder<Vector4, NoOptions, Vector4MotionAdapter> Create(MotionSettings<Vector4, NoOptions> settings)
155 => Create<Vector4, NoOptions, Vector4MotionAdapter>(settings);
156
157 public static MotionBuilder<Quaternion, NoOptions, QuaternionMotionAdapter> Create(MotionSettings<Quaternion, NoOptions> settings)
158 => Create<Quaternion, NoOptions, QuaternionMotionAdapter>(settings);
159
160 public static MotionBuilder<Color, NoOptions, ColorMotionAdapter> Create(MotionSettings<Color, NoOptions> settings)
161 => Create<Color, NoOptions, ColorMotionAdapter>(settings);
162
163 public static MotionBuilder<Rect, NoOptions, RectMotionAdapter> Create(MotionSettings<Rect, NoOptions> settings)
164 => Create<Rect, NoOptions, RectMotionAdapter>(settings);
165
166 public static MotionBuilder<TValue, TOptions, TAdapter> Create<TValue, TOptions, TAdapter>(MotionSettings<TValue, TOptions> settings)
167 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions
168 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
169 {
170 if (settings == null) throw new ArgumentNullException(nameof(settings));
171 var builder = Create<TValue, TOptions, TAdapter>(settings.StartValue, settings.EndValue, settings.Duration);
172 try
173 {
174 builder.ImportEase(settings.Ease, settings.CustomEaseCurve);
175 return builder.WithOptions(settings.Options).WithLoops(settings.Loops, settings.LoopType)
176 .WithDelay(settings.Delay, settings.DelayType, settings.SkipValuesDuringDelay)
177 .WithImmediateBind(settings.ImmediateBind).WithCancelOnError(settings.CancelOnError)
178 .WithScheduler(settings.Scheduler);
179 }
180 catch { builder.Dispose(); throw; }
181 }
182 }
183 }