Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Scripts/DependencyContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,17 @@ public void InjectToSceneObjects(bool includeInactiveObjects = true)
var injectableObjects = Object.FindObjectsOfType(injectableTypeInfo.Type, includeInactiveObjects);
foreach (var injectableObject in injectableObjects)
{
if (injectableTypeInfo.IgnoreSceneInjection) continue;
// Ignore scene injection
if (injectableTypeInfo.SceneInjectionControl == SceneInjectionControl.Ignore) continue;

// Ignore if the object is inactive
if (includeInactiveObjects &&
injectableTypeInfo.SceneInjectionControl == SceneInjectionControl.OnlyWhenActive &&
injectableObject is Component obj &&
!obj.gameObject.activeInHierarchy)
{
continue;
}

// We only want the exact type here, not subclasses
// - since we will also look for them later and we don't want double injections.
Expand Down Expand Up @@ -246,12 +256,17 @@ private class InjectableTypeInfo
public bool IsMonoBehaviour { get; }
public Type Type { get; }
public IEnumerable<FieldInfo> InjectableFields { get; }
public bool IgnoreSceneInjection { get; }
public SceneInjectionControl SceneInjectionControl { get; } = SceneInjectionControl.Always;

public InjectableTypeInfo(Type type, IEnumerable<FieldInfo> injectableFields)
{
IsMonoBehaviour = type.IsSubclassOf(typeof(MonoBehaviour));
IgnoreSceneInjection = type.GetCustomAttribute<IgnoreSceneInjectionAttribute>() != null;
var sceneInjectionControl = type.GetCustomAttribute<SceneInjectionOptions>();
if (sceneInjectionControl != null)
{
SceneInjectionControl = sceneInjectionControl.Control;
}

Type = type;
InjectableFields = injectableFields;
}
Expand Down
9 changes: 0 additions & 9 deletions Scripts/IgnoreSceneInjectionAttribute.cs

This file was deleted.

22 changes: 22 additions & 0 deletions Scripts/SceneInjectionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace UnityDependencyInjection
{
[AttributeUsage(AttributeTargets.Class)]
public class SceneInjectionOptions : Attribute
{
public SceneInjectionControl Control { get; }

public SceneInjectionOptions(SceneInjectionControl control)
{
Control = control;
}
}

public enum SceneInjectionControl
{
Ignore,
OnlyWhenActive,
Always,
}
}