Skip to content

Commit

Permalink
Fixes scene container disposal
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavopsantos committed Sep 10, 2022
1 parent e45adea commit d42630e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
14 changes: 13 additions & 1 deletion Assets/Reflex/Scripts/Extensions/SceneExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using UnityEngine.SceneManagement;
using System;
using Reflex.Scripts.Utilities;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Reflex
{
Expand All @@ -19,5 +22,14 @@ internal static bool TryFindAtRootObjects<T>(this Scene scene, out T finding)
finding = default;
return false;
}

internal static void OnUnload(this Scene scene, Action callback)
{
var gameObject = new GameObject("SceneUnloadHook");
gameObject.hideFlags = HideFlags.HideInHierarchy;
SceneManager.MoveGameObjectToScene(gameObject, scene);
var hook = gameObject.AddComponent<MonoBehaviourEventHook>();
hook.OnDestroyEvent += () => callback?.Invoke();
}
}
}
9 changes: 1 addition & 8 deletions Assets/Reflex/Scripts/Injectors/SceneInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ internal static void Inject(Scene scene, ContainerStack containerStack)
var container = containerStack.PushNew();
sceneContext.InstallBindings(container);
container.InstantiateNonLazySingletons();

void DisposeScopedContainer(Scene _)
{
containerStack.Pop().Dispose();
SceneManager.sceneUnloaded -= DisposeScopedContainer;
}

SceneManager.sceneUnloaded += DisposeScopedContainer;
scene.OnUnload(() => containerStack.Pop().Dispose());
}

foreach (var monoBehaviour in GetEveryMonoBehaviourAtScene(scene))
Expand Down
15 changes: 15 additions & 0 deletions Assets/Reflex/Scripts/Utilities/MonoBehaviourEventHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using UnityEngine;

namespace Reflex.Scripts.Utilities
{
public class MonoBehaviourEventHook : MonoBehaviour
{
public event Action OnDestroyEvent;

private void OnDestroy()
{
OnDestroyEvent?.Invoke();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Reflex/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.gustavopsantos.reflex",
"displayName": "Reflex",
"version": "2.0.1",
"version": "2.0.2",
"unity": "2019.3",
"description": "Minimal dependency injection framework for Unity",
"keywords": ["Dependency Injection", "IoC", "DI", "DI Container"],
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ EditorBuildSettings:
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/SceneContextSandbox/Scenes/SceneOne.unity
path: Assets/Reflex.Sandbox/Scenes/SceneOne.unity
guid: f528ad3d355a5ef488f900115329f198
- enabled: 1
path: Assets/SceneContextSandbox/Scenes/SceneTwo.unity
path: Assets/Reflex.Sandbox/Scenes/SceneTwo.unity
guid: 5fb3905581a42cd4bbf5694b5942af33
m_configObjects: {}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Reflex is an [Dependency Injection](https://stackify.com/dependency-injection/)

### Install via UPM (using Git URL)
```json
"com.gustavopsantos.reflex": "https://github.com/gustavopsantos/reflex.git?path=/Assets/Reflex/#2.0.1"
"com.gustavopsantos.reflex": "https://github.com/gustavopsantos/reflex.git?path=/Assets/Reflex/#2.0.2"
```

### Install manually (using .unitypackage)
Expand Down

0 comments on commit d42630e

Please sign in to comment.