From 98b8227a631a3326be65072b89f1fde323505784 Mon Sep 17 00:00:00 2001 From: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com> Date: Sun, 12 Mar 2023 09:27:43 -0400 Subject: [PATCH] fix: Add Clamp extension for pre-Unity 2021 --- .../Core/SnapshotInterpolation/SnapshotInterpolation.cs | 4 ++++ Assets/Mirror/Core/Tools/Extensions.cs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs b/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs index d36de8b2d..36387608d 100644 --- a/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs +++ b/Assets/Mirror/Core/SnapshotInterpolation/SnapshotInterpolation.cs @@ -130,7 +130,11 @@ public static double TimelineClamp( // outside of the area, we clamp. double lowerBound = targetTime - bufferTime; double upperBound = targetTime + bufferTime; +#if !UNITY_2021_OR_NEWER + return Extensions.Clamp(localTimeline, lowerBound, upperBound); +#else return Math.Clamp(localTimeline, lowerBound, upperBound); +#endif } // call this for every received snapshot. diff --git a/Assets/Mirror/Core/Tools/Extensions.cs b/Assets/Mirror/Core/Tools/Extensions.cs index 02407e09f..ca394e8bb 100644 --- a/Assets/Mirror/Core/Tools/Extensions.cs +++ b/Assets/Mirror/Core/Tools/Extensions.cs @@ -59,6 +59,14 @@ public static bool TryDequeue(this Queue source, out T element) element = default; return false; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Clamp(this T val, T min, T max) where T : IComparable + { + if (val.CompareTo(min) < 0) return min; + else if (val.CompareTo(max) > 0) return max; + else return val; + } #endif } }