Hey everyone!
I recently discovered the addressables and decided to migrate to use it. And I am now stuck at my first step - a correct transition between scenes. After each transition, I get my scene count in the profiler increased. Looks like a memory leak.
I started with a basic two-scene project where first LoadingScene is included in the player build and another GameScene is addressable. Each has its own MonoBehaviour script.
public class LoadingScene : MonoBehaviour
{
[SerializeField] private AssetReference scene;
private float _next;
private void Start()
{
_next = Time.time + 8f;
}
private void Update()
{
if (_next < Time.time)
{
_next = Time.time + 8f;
Addressables.LoadSceneAsync(scene, LoadSceneMode.Single, true);
}
}
}
public class GameScene : MonoBehaviour
{
[SerializeField] private string scenePath;
private float _next;
private void Start()
{
_next = Time.time + 3f;
}
private void Update()
{
if (_next < Time.time)
{
_next = Time.time + 3f;
SceneManager.LoadScene(scenePath, LoadSceneMode.Single);
}
}
}
After each transition from the loading scene to the game scene in Profiler I get the scene count increased, but after the transition back from the game scene to the loading scene scene count doesn’t decrease.
If GameScene is loaded in Additive mode and unloaded using Addressables.UnloadSceneAsync scene count is correctly decreased.
As I understood from addressable docs, an addressable scene will unload after opening another scene in a single mode, just like I do.
After some debugging and code inspection, I stopped at
UnityEngine.ResourceManagement.ResourceProviders.SceneProvider.UnloadSceneOp
protected override void Execute()
{
if (m_sceneLoadHandle.IsValid() && m_Instance.Scene.isLoaded)
{
#if ENABLE_ADDRESSABLE_PROFILER && UNITY_2021_2_OR_NEWER
Profiling.ProfilerRuntime.SceneReleased(m_sceneLoadHandle);
#endif
var unloadOp = SceneManager.UnloadSceneAsync(m_Instance.Scene, m_UnloadOptions);
if (unloadOp == null)
UnloadSceneCompletedNoRelease(null);
else
unloadOp.completed += UnloadSceneCompletedNoRelease;
}
else
UnloadSceneCompleted(null);
HasExecuted = true;
}
Looks like in the case using Addressables.UnloadSceneAsync condition m_Instance.Scene.isLoaded is true so statement Profiling.ProfilerRuntime.SceneReleased(m_sceneLoadHandle) executed. And the opposite in the case using SceneManager.LoadScene(scenePath, LoadSceneMode.Single)
And now I wonder if is it a profiler issue or if am I doing something wrong with LoadSceneMode.Single.
There are not many tutorials for scene transitions yet so, I am asking for help here. Is it actually a memory leak and if it is, how to fix it?