Skip to content

Commit

Permalink
Scene Loader update
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-osipov committed May 2, 2020
1 parent 5c96a04 commit 5337290
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions Runtime/Patterns/Scene/SceneLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace StansAssets.Foundation.Patterns
Expand All @@ -13,6 +14,7 @@ public static class SceneLoader
public static event Action<Scene, LoadSceneMode> SceneLoaded = delegate { };

static readonly List<Scene> s_AdditiveScenes = new List<Scene>();
static readonly Dictionary<string, AsyncOperation> s_LoadSceneOperations = new Dictionary<string, AsyncOperation>();
static readonly Dictionary<string, List<Action<Scene>>> s_LoadSceneRequests = new Dictionary<string, List<Action<Scene>>>();
static readonly Dictionary<string, List<Action>> s_UnloadSceneCallbacks = new Dictionary<string, List<Action>>();

Expand All @@ -27,24 +29,39 @@ static SceneLoader()
/// <param name="sceneName">Name of the scene to be loaded.</param>
/// <param name="loadCompleted">Load Completed callback.</param>
/// </summary>
public static void LoadAdditively(string sceneName, Action<Scene> loadCompleted = null)
public static AsyncOperation LoadAdditively(string sceneName, Action<Scene> loadCompleted = null)
{
if (!s_LoadSceneRequests.ContainsKey(sceneName))
{
List<Action<Scene>> callbacks = null;
var callbacks = new List<Action<Scene>>();
if (loadCompleted != null)
callbacks = new List<Action<Scene>> { loadCompleted };
callbacks.Add(loadCompleted);


var loadAsyncOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
s_LoadSceneRequests.Add(sceneName, callbacks);
s_LoadSceneOperations.Add(sceneName, loadAsyncOperation);
return loadAsyncOperation;
}
else
{
if (loadCompleted != null) {
var callbacks = s_LoadSceneRequests[sceneName] ?? new List<Action<Scene>>();
callbacks.Add(loadCompleted);
s_LoadSceneRequests[sceneName] = callbacks;
}

if (loadCompleted != null) {
var callbacks = s_LoadSceneRequests[sceneName] ?? new List<Action<Scene>>();
callbacks.Add(loadCompleted);
s_LoadSceneRequests[sceneName] = callbacks;
}
SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

return s_LoadSceneOperations[sceneName];
}

/// <summary>
/// Current scene load async operation. Can be used to check if scene is loaded or current load progress.
/// </summary>
/// <param name="sceneName">Name of the scene.</param>
public static AsyncOperation GetLoadProgress(string sceneName)
{
return s_LoadSceneOperations.TryGetValue(sceneName, out var asyncOperation)
? asyncOperation
: null;
}

/// <summary>
Expand Down Expand Up @@ -116,6 +133,8 @@ static void SceneUnloadComplete(Scene scene)

s_UnloadSceneCallbacks.Remove(scene.name);
}

s_LoadSceneOperations.Remove(scene.name);
}
}
}

0 comments on commit 5337290

Please sign in to comment.