public static void SafeWhile(System.Func <bool> loopContent, int maxIterations = 1000, bool logWarning = true, UnityEngine.Object logContext = null) { if (loopContent.IsNull()) { Debug.LogWarning($"SafeWhile loop exited due to null {nameof(loopContent)}", logContext); return; } int _iterationCount = 0; while (loopContent.Invoke() == true) { _iterationCount++; if (_iterationCount < maxIterations) { continue; } if (logWarning) { Debug.LogWarning($"SafeWhile loop broken out of, exceeded max iterations ({maxIterations})", logContext); } break; } }
// == EXPENSIVE == // // Only use to test while loop logic so Unity doesn't brick on you // // =============== // #region SafeWhile public static void SafeWhile(System.Func <bool> predicate, System.Action action, int maxIterations = 1000, bool logWarning = true, UnityEngine.Object logContext = null) { if (predicate.IsNull()) { Debug.LogWarning($"SafeWhile loop exited due to a null predicate", logContext); return; } if (action.IsNull()) { Debug.LogWarning($"SafeWhile loop exited due to a null action", logContext); return; } SafeWhile(() => { bool _valid = predicate.Invoke(); if (_valid) { action?.Invoke(); } return(_valid); }, maxIterations, logWarning, logContext); }