Cleanup when scene is unloaded

I’d like to have some object that will be called when a scene is unloaded, to perform certain cleanups.

Is there any deterministic way of running code when a scene unloads?

By deterministic, i mean - controlling exactly WHEN the cleanup code runs (e.g: before any of the other scene objects’ OnDestroy, for example).

So far i have an object in the scene with a script that handles OnDestroy, but there’s no guarantee to control when that code actually will get called.

Why not just wrap your scene loading method with a manager that dispatches a message before calling LoadScene?

Something along these lines

Class SceneManager
{
   public delegate void SceneLoadEvent();
   public static event SceneLoadEvent OnBeforeSceneLoad;

   public static void LoadScene( string sceneName )
   {
      if ( OnBeforeSceneLoad != null )
      {
         OnBeforeSceneLoad();
      }
      
      Application.LoadScene( sceneName );
   }
}