stronginject icon indicating copy to clipboard operation
stronginject copied to clipboard

Add Unity3D example (similar to examples of Zenject)

Open dzmitry-lahoda opened this issue 3 years ago • 10 comments

dzmitry-lahoda avatar Aug 14 '20 12:08 dzmitry-lahoda

What do you mean by a unity example?

YairHalberstadt avatar Aug 14 '20 12:08 YairHalberstadt

i can do it, but only if i again will get to Unity3D project, not so soon so.

dzmitry-lahoda avatar Aug 14 '20 18:08 dzmitry-lahoda

Unity doesn't currently support Source Generators: https://forum.unity.com/threads/source-generators-via-roslyn-analyzers-in-2020-2.1003386/

I will put this on hold until that happens.

YairHalberstadt avatar Dec 02 '20 04:12 YairHalberstadt

Looking forward to this, evidently Unity has a stretch goal for 2021.2 to make source generators available so fingers crossed.

upscalebaby avatar May 07 '21 04:05 upscalebaby

Once this is merged, and the new version is out you can create a Untiy sample https://github.com/xoofx/UnityNuGet/pull/77 Using StrongInject requires unity 2021.2 and later

KuraiAndras avatar Nov 12 '21 12:11 KuraiAndras

Do you have a link to the examples of zenject? I can't find them...

YairHalberstadt avatar Dec 05 '21 09:12 YairHalberstadt

Two ways to get them:

  1. Download latest release, they are in the optional extras folder once you import the package in unity https://github.com/modesttree/Zenject/releases
  2. This folder in the source, see the two folder Samplegame1 and SampleGame2 https://github.com/modesttree/Zenject/tree/master/UnityProject/Assets/Plugins/Zenject/OptionalExtras

I think what's mainly interesting is to see the stronginject version of the Installers: https://github.com/modesttree/Zenject/tree/master/UnityProject/Assets/Plugins/Zenject/OptionalExtras/SampleGame2%20(Advanced)/Scripts/Installers

as this is where the actual Bindins happen. The main issue with Unity is probably the Binding to existing scene objects (which should be possible with Container Properties/fields), the injection of existing objects in the scene as well as the creation of new objects that need to be injected afterwards.

The problem is that Unity MonoBehaviours do not support constructor injection at all. Those need to be added by the Unity System and then initialized afterwards.

I'm pretty sure its already possible to do all those things with StrongInject right now most likely through custom factories for MonoBehaviours/Prefabs.

Blackclaws avatar Mar 16 '22 11:03 Blackclaws

I gave it a try, but I have zero experience with unity, and it looks like you need to use the unity editor to work with unity projects. I don't think it's an effective use of my time to learn unity just for this, so if someone who's more familiar with unity wants to go for it, that would be great!

YairHalberstadt avatar Mar 21 '22 06:03 YairHalberstadt

I have experience with DI in unity, I even wrote a library for this (https://kuraiandras.github.io/Injecter/documentation/injecter.unity.html). The problem is that in Unity the root classes that you create are inheriting from MonoBehaviour and you can't use new with them, also you can't use their constructors - so constructor injection will not work with them. In my library (similar to zenject) I solved the problem by using attributes on fields and properties, and setting them at runtime. The problem is that as far as I understand this is not possible with stronginject. In fact, I tried to create a sample with stronginject, but I could not solve this problem.

KuraiAndras avatar Mar 21 '22 06:03 KuraiAndras

If I have a bit of spare time I might try to hack something together.

Kurai is right that MonoBehaviours need injecting via functions, I think that this can be achieved via strong injects methods/properties behaviour though/factories.

It would be interesting to know whether there is any mechanism that StrongInject would directly support that allows injecting already created objects via some Inject function.

There is also no issue with having a manual root MonoBehaviour and then building a class hierarchy of standard classes from there.

Blackclaws avatar Mar 21 '22 07:03 Blackclaws