diff --git a/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/Context.cs b/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/Context.cs index 20e5ae939..90ea09601 100644 --- a/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/Context.cs +++ b/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/Context.cs @@ -262,7 +262,7 @@ protected void InstallSceneBindings(List injectableMonoBehaviours } } - if (binding.Context == this) + if (binding.Context == this || binding.ParentContext == this) { InstallZenjectBinding(binding); } diff --git a/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/SceneContext.cs b/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/SceneContext.cs index ceba02744..274e55248 100644 --- a/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/SceneContext.cs +++ b/UnityProject/Assets/Plugins/Zenject/Source/Install/Contexts/SceneContext.cs @@ -321,6 +321,21 @@ void InstallBindings(List injectableMonoBehaviours) { decoratorContext.InstallDecoratorSceneBindings(); } + + foreach (var binding in Resources.FindObjectsOfTypeAll()) + { + var parent = binding.transform.parent; + if (parent == null) + continue; + + var siblingContext = binding.GetComponent(); + var parentContext = parent.GetComponentInParent(); + + if (siblingContext != null && parentContext != null && binding.UseParentContext) + { + binding.ParentContext = parentContext; + } + } InstallSceneBindings(injectableMonoBehaviours); diff --git a/UnityProject/Assets/Plugins/Zenject/Source/Install/ZenjectBinding.cs b/UnityProject/Assets/Plugins/Zenject/Source/Install/ZenjectBinding.cs index fcdb386c5..fd2a95c78 100644 --- a/UnityProject/Assets/Plugins/Zenject/Source/Install/ZenjectBinding.cs +++ b/UnityProject/Assets/Plugins/Zenject/Source/Install/ZenjectBinding.cs @@ -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; } @@ -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