diff --git a/API/RenderSession.cs b/API/RenderSession.cs index 3ef1286..3623847 100644 --- a/API/RenderSession.cs +++ b/API/RenderSession.cs @@ -39,6 +39,7 @@ public class RenderSession private Func? _endCondition; private Action? _onComplete; + private Action? _onLastUpdate; /// /// Sets whether auto mode is enabled. @@ -114,6 +115,17 @@ public class RenderSession return this; } + /// + /// Adds a callback action to be invoked on the last update of each frame in the render session. + /// + /// The callback action to execute. + /// The current instance for method chaining. + public RenderSession OnLastUpdate(Action callback) + { + _onLastUpdate += callback; + return this; + } + /// /// Applies the configured flags and initiates the render session. /// @@ -130,6 +142,7 @@ public class RenderSession if (_endCondition is not null) RenderContext.EndCondition = _endCondition; if (_onComplete is not null) RenderContext.OnComplete = _onComplete; + if (_onLastUpdate is not null) RenderContext.OnLastUpdate = _onLastUpdate; RenderManager.Instance.levelPath = levelPath; RenderManager.Instance.Configuration = renderConfig; diff --git a/Core/FirstUpdateHook.cs b/Core/FirstUpdateHook.cs index caa6caf..b0d5d7e 100644 --- a/Core/FirstUpdateHook.cs +++ b/Core/FirstUpdateHook.cs @@ -33,7 +33,7 @@ internal class FirstUpdateHook : MonoBehaviour if (RenderManager.Instance) { - RenderManager.Instance.OnPreRenderUpdate(); + RenderManager.Instance.OnPreUpdate(); } } diff --git a/Core/RenderManager.cs b/Core/RenderManager.cs index 6683193..0df152e 100644 --- a/Core/RenderManager.cs +++ b/Core/RenderManager.cs @@ -48,6 +48,9 @@ internal static class RenderContext }; public static Action OnComplete = OnCompleteDefault; + private static readonly Action OnLastUpdateDefault = () => { }; + public static Action OnLastUpdate = OnLastUpdateDefault; + public static void Reset() { Auto = true; @@ -58,6 +61,7 @@ internal static class RenderContext EndCondition = EndConditionDefault; OnComplete = OnCompleteDefault; + OnLastUpdate = OnLastUpdateDefault; } } @@ -270,7 +274,7 @@ internal class RenderManager : MonoBehaviour } } - public void OnPreRenderUpdate() + public void OnPreUpdate() { if (!IsRendering) return; @@ -331,6 +335,8 @@ internal class RenderManager : MonoBehaviour { floor.holdRenderer.m_meshRenderer.enabled = false; } + + TriggerOnLastUpdate(); } // ReSharper disable once Unity.IncorrectMethodSignature @@ -867,6 +873,18 @@ internal class RenderManager : MonoBehaviour Melon.Logger.Error($"OnComplete callback caused error: {err}"); } } + + private static void TriggerOnLastUpdate() + { + try + { + RenderContext.OnLastUpdate.Invoke(); + } + catch (Exception err) + { + Melon.Logger.Error($"OnLastUpdate callback caused error: {err}"); + } + } private static readonly Dictionary AudioCodecMatch = new() {