Skip to content

Commit ee34f4e

Browse files
committed
add documentation on new inheritable type
1 parent bdd1be6 commit ee34f4e

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

Documentation/documentation/injecter.unity.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ The `Injecter.Unity` lets you set up the following flow:
1414

1515
- A "composition root" is initialized part of the entry point of the application
1616
- 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
2024

2125
## Getting started
2226

@@ -80,7 +84,9 @@ public static class AppInstaller
8084
}
8185
```
8286

83-
### Inject into `MonoBehaviours`
87+
### Inject using the helper components
88+
89+
#### Inject into `MonoBehaviours`
8490

8591
Create a script which will receive injection
8692

@@ -92,16 +98,40 @@ public class MyScript : MonoBehaviour
9298
}
9399
```
94100

95-
### Add `MonoInjector`
101+
#### Add `MonoInjector`
96102

97103
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 / ...`
98104

105+
### Inject by inheriting from `MonoBehaviourInjected`
106+
107+
Create a script which will receive injection
108+
109+
```csharp
110+
public class MyScript : MonoBehaviourInjected
111+
{
112+
[Inject] private readonly IMyService _service = default!;
113+
114+
protected override void Awake()
115+
{
116+
base.Awake();
117+
118+
// Custom logic goes here
119+
}
120+
121+
protected override void OnDestroy()
122+
{
123+
// Custom cleanup logic goes herer
124+
125+
base.OnDestroy();
126+
}
127+
}
128+
```
129+
99130
## Manual injection
100131

101132
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:
102133

103134
```csharp
104-
105135
var myObject = new GameObject("MyObject");
106136
myObject.AddComponent<TestComponent>();
107137
myObject.AddComponent<MonoInjector>();
@@ -110,7 +140,6 @@ public class MyComponent : MonoBehaviour
110140
{
111141
[Inject] private readonly MyService _service = default!;
112142
}
113-
114143
```
115144

116145
## Testing
@@ -157,7 +186,11 @@ public IEnumerator My_Test_Does_Stuff()
157186
> [!NOTE]
158187
> You can also do the same in the test's `Setup` or `Teardown` stage
159188
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:
161194

162195
1. Set up a composition root as described above.
163196
2. Remove inheriting from the old `MonoBehaviourInjected` and similar classes

0 commit comments

Comments
 (0)