The point is the usage of DI-container in a proper way.
Add in Packages/manifest.json to dependencies:
"com.danand.simplecontainer": "https://github.com/Danand/SimpleContainer.git?path=SimpleContainer.Unity/Assets#0.7.1-beta.4",From local repository
"com.danand.simplecontainer": "file:///D/repos/SimpleContainer/.git?path=SimpleContainer.Unity/Assets#0.7.1-beta.4",
From local working copy
"com.danand.simplecontainer": "file:D:/repos/SimpleContainer/SimpleContainer.Unity/Assets",
What is the difference?
Local repository is resolved just like normal Git repository with optionally specified revision.
Local working copy is being copied "as is" into dependent project, without running any Git process.
- Add
UnityProjectRootcomponent to any object in hierarchy. - Reference
SimpleContainer.CoreandSimpleContainer.Unityassemblies to your installer assembly via Assembly Definitions). - Implement your own
MonoInstallerand add it as component too (example project below). - Reference
MonoInstallertoUnityProjectRootvia inspector. - Run.
- Do not use
DontDestroyOnLoad()to pass container through different scenes. UseLoadSceneMode.Singlefor "immortal" objects (e.g. managers, services, etc.), andLoadSceneMode.Additivefor presentation layer (e.g. gameplay, UI, etc.). Quite good alternative is to use separateUnityProjectRootper each loaded scene. - Prefer constructor injection over any other. Yeah, that means less usage of
MonoBehaviourdependants. - Do not put dependencies on container. It will be perfect, if you did not have any
using SimpleContainer;in your needy classes. - Use custom
Injectattribute (example below). - You may move your installers to a separate assembly (via
.asmdef) for stronger container usage protection. - If you really want to inject into
MonoBehaviourwithout explicit registration – useMonoRegistrator.
See example project included into this repository. Also you may open it with Unity and run.
See contribution note.
- Clean up repository
- Set up CI
- Optimize resolving
- Refactor container interface