Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Use Parent Context option to ZenjectBinding #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ protected void InstallSceneBindings(List<MonoBehaviour> injectableMonoBehaviours
}
}

if (binding.Context == this)
if (binding.Context == this || binding.ParentContext == this)
{
InstallZenjectBinding(binding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,21 @@ void InstallBindings(List<MonoBehaviour> injectableMonoBehaviours)
{
decoratorContext.InstallDecoratorSceneBindings();
}

foreach (var binding in Resources.FindObjectsOfTypeAll<ZenjectBinding>())
{
var parent = binding.transform.parent;
if (parent == null)
continue;

var siblingContext = binding.GetComponent<GameObjectContext>();
var parentContext = parent.GetComponentInParent<Context>();

if (siblingContext != null && parentContext != null && binding.UseParentContext)
{
binding.ParentContext = parentContext;
}
}

InstallSceneBindings(injectableMonoBehaviours);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class ZenjectBinding : MonoBehaviour
[SerializeField]
BindTypes _bindType = BindTypes.Self;

[Tooltip("When set and this binding is on the same object as a GameObjectContext, this will bind the given components to it as normal but also any parent context. This is useful when using ZenjectBinding to bind a nested facade which has its own GameObjectContext. If your ZenjectBinding is for a component that is not a nested facade then it is not necessary to check this.")]
[SerializeField]
bool _useParentContext;

Context _parentContext;

public bool UseSceneContext
{
get { return _useSceneContext; }
Expand All @@ -54,6 +60,18 @@ public BindTypes BindType
get { return _bindType; }
}

public bool UseParentContext
{
get => _useParentContext;
set => _useParentContext = value;
}

public Context ParentContext
{
get => _parentContext;
set => _parentContext = value;
}

public void Start()
{
// Define this method so we expose the enabled check box
Expand Down