Skip to content

Commit

Permalink
feat: scene service now also returns a scene.
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-osipov committed Jan 24, 2021
1 parent 3efc323 commit 59ba262
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
16 changes: 14 additions & 2 deletions com.stansassets.scene-management/Runtime/Core/SceneActionsQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using StansAssets.Foundation.Async;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace StansAssets.SceneManagement
{
Expand All @@ -18,6 +19,8 @@ public class SceneActionsQueue
readonly Queue<SceneAction> m_ActionsQueue = new Queue<SceneAction>();

public Dictionary<string, ISceneManager> AvailableSceneManagers { get; } = new Dictionary<string, ISceneManager>();
public Dictionary<string, Scene> LoadedScenes { get; } = new Dictionary<string, Scene>();


public IEnumerable<SceneAction> ScheduledActions => m_ActionsQueue;

Expand Down Expand Up @@ -50,6 +53,7 @@ public void AddAction(SceneActionType type, string sceneName)
public void Start(Action<float> onProgress = null, Action onComplete = null)
{
AvailableSceneManagers.Clear();
LoadedScenes.Clear();
OnComplete = onComplete;
OnProgress = onProgress;
if (m_Preloader != null)
Expand Down Expand Up @@ -84,13 +88,19 @@ public T GetLoadedSceneManager<T>() where T : ISceneManager
foreach (var kvp in AvailableSceneManagers)
{
var sceneManager = kvp.Value;
if (sceneManager.GetType() == typeof(T))
var sceneManagerType = sceneManager.GetType();
if (sceneManagerType == typeof(T) || typeof(T).IsAssignableFrom(sceneManagerType))
return (T)sceneManager;
}

return default;
}

public Scene GetLoadedScene(string sceneName)
{
return LoadedScenes[sceneName];
}

public IEnumerator OnStackProgress()
{
while (m_IsRunning)
Expand Down Expand Up @@ -126,11 +136,12 @@ void ExecuteActionsStack(Action onComplete)
switch (actionData.Type)
{
case SceneActionType.Load:
m_SceneLoadService.Load<ISceneManager>(actionData.SceneName, sceneManager =>
m_SceneLoadService.Load<ISceneManager>(actionData.SceneName, (scene, sceneManager) =>
{
if (sceneManager != null)
AvailableSceneManagers[actionData.SceneName] = sceneManager;

LoadedScenes.Add(actionData.SceneName, scene);
ExecuteActionsStack(onComplete);
});

Expand All @@ -149,6 +160,7 @@ void ExecuteActionsStack(Action onComplete)
m_SceneLoadService.Unload(actionData.SceneName, () =>
{
AvailableSceneManagers.Remove(actionData.SceneName);
LoadedScenes.Remove(actionData.SceneName);
ExecuteActionsStack(onComplete);
});
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ namespace StansAssets.SceneManagement
{
public class SceneLoadService : ISceneLoadService
{
public void Load<T>(IScenePreloader preloader, string sceneName, Action<T> onComplete) where T : ISceneManager
public void Load<T>(IScenePreloader preloader, string sceneName, Action<Scene, T> onComplete) where T : ISceneManager
{
preloader.FadeIn(() =>
{
Load<T>(sceneName, sceneManager =>
Load<T>(sceneName, (scene, sceneManager) =>
{
preloader.FadeOut(() =>
{
onComplete?.Invoke(sceneManager);
onComplete?.Invoke(scene, sceneManager);
});
});
});
}

public void Load<T>(string sceneName, Action<T> onComplete) where T : ISceneManager
public void Load<T>(string sceneName, Action<Scene, T> onComplete) where T : ISceneManager
{
AdditiveScenesLoader.LoadAdditively(sceneName, scene =>
{
Expand All @@ -30,12 +30,12 @@ public void Load<T>(string sceneName, Action<T> onComplete) where T : ISceneMana
{
sceneDelegate.ActivateScene(() =>
{
onComplete?.Invoke(sceneManager);
onComplete?.Invoke(scene, sceneManager);
});
}
else
{
onComplete?.Invoke(sceneManager);
onComplete?.Invoke(scene, sceneManager);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using UnityEngine.SceneManagement;

namespace StansAssets.SceneManagement
{
public interface ISceneLoadService
{
void Load<T>(IScenePreloader preloader, string sceneName, Action<T> onComplete) where T : ISceneManager;
void Load<T>(string sceneName, Action<T> onComplete) where T : ISceneManager;
void Load<T>(IScenePreloader preloader, string sceneName, Action<Scene, T> onComplete) where T : ISceneManager;
void Load<T>(string sceneName, Action<Scene, T> onComplete) where T : ISceneManager;
void Deactivate<T>(string sceneName, Action<T> onComplete) where T : ISceneManager;
void Unload(string sceneName, Action onComplete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace StansAssets.SceneManagement
{
public interface IApplicationState<T> where T : Enum
{
void ChangeState(StackChangeEvent<T> evt, IProgressReporter reporter);
void ChangeState(StackChangeEvent<T> evt, IProgressReporter progressReporter);
}
}

0 comments on commit 59ba262

Please sign in to comment.