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 IsActiveSelf binding option #660

Open
wants to merge 2 commits 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 @@ -187,6 +187,25 @@ public IEnumerator TestUnderTransformGetter()
yield break;
}

[UnityTest]
public IEnumerator TestIsActiveSelf()
{
PreInstall();

Container.Bind<Foo>().FromNewComponentOnNewGameObject()
.IsActiveSelf(false)
.AsSingle()
.NonLazy();

var activeSelf = Container.Resolve<Foo>().gameObject.activeSelf;

PostInstall();

Assert.That(!activeSelf, "activeSelf should be false");

yield break;
}

public interface IBar
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ public IEnumerator TestCircularDependencies()
yield break;
}

[UnityTest]
public IEnumerator TestIsActiveSelf()
{
PreInstall();

Container.Bind<Foo>().FromComponentInNewPrefab(FooPrefab)
.IsActiveSelf(false)
.AsSingle()
.NonLazy();

var activeSelf = Container.Resolve<Foo>().gameObject.activeSelf;

PostInstall();

Assert.That(!activeSelf, "activeSelf should be false");

yield break;
}

GameObject GetPrefab(string name)
{
return FixtureUtil.GetPrefab("TestFromPrefab/{0}".Fmt(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public Quaternion? Rotation
set;
}

public bool? IsActiveSelf
{
get;
set;
}

public static readonly GameObjectCreationParameters Default = new GameObjectCreationParameters();

public override int GetHashCode()
Expand All @@ -57,6 +63,7 @@ public override int GetHashCode()
hash = hash * 29 + (ParentTransformGetter == null ? 0 : ParentTransformGetter.GetHashCode());
hash = hash * 29 + (!Position.HasValue ? 0 : Position.Value.GetHashCode());
hash = hash * 29 + (!Rotation.HasValue ? 0 : Rotation.Value.GetHashCode());
hash = hash * 29 + (!IsActiveSelf.HasValue ? 0 : IsActiveSelf.Value.GetHashCode());
return hash;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if !NOT_UNITY3D

namespace Zenject
{
[NoReflectionBaking]
public class ActiveGameObjectScopeConcreteIdArgConditionCopyNonLazyBinder : ScopeConcreteIdArgConditionCopyNonLazyBinder
{
protected GameObjectCreationParameters GameObjectInfo { get; }

public ActiveGameObjectScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo bindInfo, GameObjectCreationParameters gameObjectInfo) :
base(bindInfo)
{
GameObjectInfo = gameObjectInfo;
}

public ScopeConcreteIdArgConditionCopyNonLazyBinder IsActiveSelf(bool isActive)
{
GameObjectInfo.IsActiveSelf = isActive;
return this;
}
}
}

#endif

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,28 @@
namespace Zenject
{
[NoReflectionBaking]
public class TransformScopeConcreteIdArgConditionCopyNonLazyBinder : ScopeConcreteIdArgConditionCopyNonLazyBinder
public class TransformScopeConcreteIdArgConditionCopyNonLazyBinder : ActiveGameObjectScopeConcreteIdArgConditionCopyNonLazyBinder
{
public TransformScopeConcreteIdArgConditionCopyNonLazyBinder(
BindInfo bindInfo,
GameObjectCreationParameters gameObjectInfo)
: base(bindInfo)
: base(bindInfo, gameObjectInfo)
{
GameObjectInfo = gameObjectInfo;
}

protected GameObjectCreationParameters GameObjectInfo
{
get;
private set;
}

public ScopeConcreteIdArgConditionCopyNonLazyBinder UnderTransform(Transform parent)
public ActiveGameObjectScopeConcreteIdArgConditionCopyNonLazyBinder UnderTransform(Transform parent)
{
GameObjectInfo.ParentTransform = parent;
return this;
}

public ScopeConcreteIdArgConditionCopyNonLazyBinder UnderTransform(Func<InjectContext, Transform> parentGetter)
public ActiveGameObjectScopeConcreteIdArgConditionCopyNonLazyBinder UnderTransform(Func<InjectContext, Transform> parentGetter)
{
GameObjectInfo.ParentTransformGetter = parentGetter;
return this;
}

public ScopeConcreteIdArgConditionCopyNonLazyBinder UnderTransformGroup(string transformGroupname)
public ActiveGameObjectScopeConcreteIdArgConditionCopyNonLazyBinder UnderTransformGroup(string transformGroupname)
{
GameObjectInfo.GroupName = transformGroupname;
return this;
Expand Down
21 changes: 20 additions & 1 deletion UnityProject/Assets/Plugins/Zenject/Source/Main/DiContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,15 @@ public Type ResolveType(Type type)
}
}

public Type TryResolveType(Type type)
{
using (var context = ZenPools.SpawnInjectContext(this, type))
{
context.Optional = true;
return ResolveType(context);
}
}

// Returns the concrete type that would be returned with Resolve(context)
// without actually instantiating it
// This is safe to use within installers
Expand All @@ -896,6 +905,11 @@ public Type ResolveType(InjectContext context)

if (providerInfo == null)
{
if (context.Optional)
{
return null;
}

throw Assert.CreateException(
"Unable to resolve {0}{1}. Object graph:\n{2}", context.BindingId,
(context.ObjectType == null ? "" : " while building object with type '{0}'".Fmt(context.ObjectType)),
Expand Down Expand Up @@ -1692,7 +1706,7 @@ internal GameObject CreateAndParentPrefab(

var prefabWasActive = prefabAsGameObject.activeSelf;

shouldMakeActive = prefabWasActive;
shouldMakeActive = gameObjectBindInfo.IsActiveSelf ?? prefabWasActive;

var parent = GetTransformGroup(gameObjectBindInfo, context);

Expand Down Expand Up @@ -1797,6 +1811,11 @@ public GameObject CreateEmptyGameObject(
FlushBindings();

var gameObj = new GameObject(gameObjectBindInfo.Name ?? "GameObject");
if (gameObjectBindInfo.IsActiveSelf.HasValue)
{
gameObj.SetActive(gameObjectBindInfo.IsActiveSelf.Value);
}

var parent = GetTransformGroup(gameObjectBindInfo, context);

if (parent == null)
Expand Down