You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: Documentation/documentation/injecter.unity.md
+41-8
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,13 @@ The `Injecter.Unity` lets you set up the following flow:
14
14
15
15
- A "composition root" is initialized part of the entry point of the application
16
16
- Create a script which needs to be injected
17
-
- Add `MonoInjector` to the `GameObject` hosting the script
18
-
-`MonoInjector` runs at `Awake`, and it's execution order (`int.MinValue` - first) is run before your own component's `Awake`. Every injected script will have it's own `IServiceScope` derived from the root scope. This scope can be retrieved through the `IScopeStore`, and the owner of the scope is the script being injected
19
-
- When the `GameObject` is destroyed, `MonoDisposer` will run during the `OnDestroy` method, with an execution order of `int.MaxValue` - last
17
+
- Choose an injection method:
18
+
- Either use the helper components
19
+
- Add `MonoInjector` to the `GameObject` hosting the script
20
+
-`MonoInjector` runs at `Awake`, and it's execution order (`int.MinValue` - first) is run before your own component's `Awake`. Every injected script will have it's own `IServiceScope` derived from the root scope. This scope can be retrieved through the `IScopeStore`, and the owner of the scope is the script being injected
21
+
- When the `GameObject` is destroyed, `MonoDisposer` will run during the `OnDestroy` method, with an execution order of `int.MaxValue` - last
22
+
- Or derive from `MonoBehaviourInjecter`
23
+
- The base class `Awake` method will do the injections, and the `OnDestroy` method will dispose of the scope
20
24
21
25
## Getting started
22
26
@@ -80,7 +84,9 @@ public static class AppInstaller
80
84
}
81
85
```
82
86
83
-
### Inject into `MonoBehaviours`
87
+
### Inject using the helper components
88
+
89
+
#### Inject into `MonoBehaviours`
84
90
85
91
Create a script which will receive injection
86
92
@@ -92,16 +98,40 @@ public class MyScript : MonoBehaviour
92
98
}
93
99
```
94
100
95
-
### Add `MonoInjector`
101
+
####Add `MonoInjector`
96
102
97
103
If you decorate your script with `[RequireComponent(typeof(MonoInjector))]` then, when adding the script to a `GameObject` the editor will add the `MonoInjector` and the `MonoDisposer` script to your `GameObject`. If for some reason this does not happen (for example, when changing an already living script into one needing injection), either add the `MonoInjector` component manually, or use the editor tools included to add the missing components to scenes or prefabs (will search all instances) through the editor menu `Tools / Injecter / ...`
98
104
105
+
### Inject by inheriting from `MonoBehaviourInjected`
When dynamically adding an injectable script to a `GameObject`, you should **not** annotate the injected script with the `RequireComponent` attribute, and add the `MonoInjector` manually, like so:
102
133
103
134
```csharp
104
-
105
135
varmyObject=newGameObject("MyObject");
106
136
myObject.AddComponent<TestComponent>();
107
137
myObject.AddComponent<MonoInjector>();
@@ -110,7 +140,6 @@ public class MyComponent : MonoBehaviour
@@ -157,7 +186,11 @@ public IEnumerator My_Test_Does_Stuff()
157
186
> [!NOTE]
158
187
> You can also do the same in the test's `Setup` or `Teardown` stage
159
188
160
-
## Migrating from `8.0.1` to `9.0.0`
189
+
## Migrating from `8.0.1` to `9.0.0` and above
190
+
191
+
If you want to continue using the inheritance setup, then change all base classes to be `MonoBehaviourInjected`. The new base class will always create scopes.
192
+
193
+
If you want to migrate to the component-based injection:
161
194
162
195
1. Set up a composition root as described above.
163
196
2. Remove inheriting from the old `MonoBehaviourInjected` and similar classes
0 commit comments