How to setup Zenject that when an interface is requested, it setups up a new game object, and returns the a...
up vote
0
down vote
favorite
Zenject is great, but I haven't found a solution that feels right, for instantiating a game object
I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"
My setup before, would have been something like this:
private static IEventsController _Events;
public static IEventsController GetEvents()
{
if (_Events == null)
{
var go = new GameObject("EventsController");
_Events = go.AddComponent<EventsController>();
}
return _Events;
}
I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.
A sample binding method call I might expect:
Container.BindComponent<IEventsController>()
.To<EventsController>()
.ViaNewGameObject("Events Controller");
- Thanks.
unity3d zenject
add a comment |
up vote
0
down vote
favorite
Zenject is great, but I haven't found a solution that feels right, for instantiating a game object
I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"
My setup before, would have been something like this:
private static IEventsController _Events;
public static IEventsController GetEvents()
{
if (_Events == null)
{
var go = new GameObject("EventsController");
_Events = go.AddComponent<EventsController>();
}
return _Events;
}
I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.
A sample binding method call I might expect:
Container.BindComponent<IEventsController>()
.To<EventsController>()
.ViaNewGameObject("Events Controller");
- Thanks.
unity3d zenject
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Zenject is great, but I haven't found a solution that feels right, for instantiating a game object
I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"
My setup before, would have been something like this:
private static IEventsController _Events;
public static IEventsController GetEvents()
{
if (_Events == null)
{
var go = new GameObject("EventsController");
_Events = go.AddComponent<EventsController>();
}
return _Events;
}
I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.
A sample binding method call I might expect:
Container.BindComponent<IEventsController>()
.To<EventsController>()
.ViaNewGameObject("Events Controller");
- Thanks.
unity3d zenject
Zenject is great, but I haven't found a solution that feels right, for instantiating a game object
I have a script, that provides various event hooks for monobehaviour events. Start, PostStart, Update, FixedUpdate, and others, so non-Unity controllers can execute in at certain events. It is based on an interface I named "IEventsController"
My setup before, would have been something like this:
private static IEventsController _Events;
public static IEventsController GetEvents()
{
if (_Events == null)
{
var go = new GameObject("EventsController");
_Events = go.AddComponent<EventsController>();
}
return _Events;
}
I'm trying to figure out if Zenject has a built in solution for this. I could use a factory or a method, but the factory has a whole extra class to manage this when Zenject might already handle it. Using a method to generate this still requires the static reference to check if its been created or not, and it feels wrong for that to sit in the installer script. Also, there is the FromComponent Series of bindings that might have something for this, but nothing I've seen so far.
A sample binding method call I might expect:
Container.BindComponent<IEventsController>()
.To<EventsController>()
.ViaNewGameObject("Events Controller");
- Thanks.
unity3d zenject
unity3d zenject
asked 6 hours ago
Dan Violet Sagmiller
1,0742823
1,0742823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Since, I can't comment to ask for elaboration I'll do my best to answer completely.
If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:
Container.Bind<IEventsController>().FromMethod(...)
If you want to construct an object that has dependencies and required injection then create a Factory class:
Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()
class EventsControllerFactory : IFactory<IEventsController> {
[Inject]
public IDep1 dep1;
public IEventsController Create() {
return new EventsController(dep1);
}
}
If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Since, I can't comment to ask for elaboration I'll do my best to answer completely.
If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:
Container.Bind<IEventsController>().FromMethod(...)
If you want to construct an object that has dependencies and required injection then create a Factory class:
Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()
class EventsControllerFactory : IFactory<IEventsController> {
[Inject]
public IDep1 dep1;
public IEventsController Create() {
return new EventsController(dep1);
}
}
If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.
add a comment |
up vote
0
down vote
Since, I can't comment to ask for elaboration I'll do my best to answer completely.
If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:
Container.Bind<IEventsController>().FromMethod(...)
If you want to construct an object that has dependencies and required injection then create a Factory class:
Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()
class EventsControllerFactory : IFactory<IEventsController> {
[Inject]
public IDep1 dep1;
public IEventsController Create() {
return new EventsController(dep1);
}
}
If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.
add a comment |
up vote
0
down vote
up vote
0
down vote
Since, I can't comment to ask for elaboration I'll do my best to answer completely.
If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:
Container.Bind<IEventsController>().FromMethod(...)
If you want to construct an object that has dependencies and required injection then create a Factory class:
Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()
class EventsControllerFactory : IFactory<IEventsController> {
[Inject]
public IDep1 dep1;
public IEventsController Create() {
return new EventsController(dep1);
}
}
If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.
Since, I can't comment to ask for elaboration I'll do my best to answer completely.
If you simply want to construct an object that has no dependencies, ie (does not need injection on itself) then:
Container.Bind<IEventsController>().FromMethod(...)
If you want to construct an object that has dependencies and required injection then create a Factory class:
Container.Bind<IEventsController>().FromFactory<EventsControllerFactory>()
class EventsControllerFactory : IFactory<IEventsController> {
[Inject]
public IDep1 dep1;
public IEventsController Create() {
return new EventsController(dep1);
}
}
If you want to create a GameObject that is also injected you have several methods that let you choose, check out the FromComponentXXX methods, that let you use a prefab reference, or a prefab stored in Resources folder, and a few others.
answered 5 hours ago
Ian Pilipski
454
454
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400749%2fhow-to-setup-zenject-that-when-an-interface-is-requested-it-setups-up-a-new-gam%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown