|
1 |
| -# Unity-ActionUpdaterService |
2 |
| - |
| 1 | +# ActionUpdaterService for Unity |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`IActionUpdaterService` is a flexible and easy-to-use service for Unity, designed to manage and dispatch update-related |
| 6 | +actions (`FixedUpdate`, `Update`, `LateUpdate`) outside of `MonoBehaviour`. It allows for a more modular approach to |
| 7 | +handling updates, especially useful in projects using dependency injection frameworks like Zenject. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Separate update methods (`FixedUpdate`, `Update`, `LateUpdate`) outside `MonoBehaviour`. |
| 12 | +- Easy subscription and unsubscription of actions. |
| 13 | +- Pause functionality to control the execution of update actions. |
| 14 | +- Automatic cleanup to prevent memory leaks. |
| 15 | + |
| 16 | +## Getting Started |
| 17 | + |
| 18 | +### Installation |
| 19 | + |
| 20 | +1. Import the `IActionUpdaterService` package into your Unity project. |
| 21 | +2. Add `ActionUpdaterService` and `ActionUpdateDispatcher` scripts to your project. |
| 22 | + |
| 23 | +### Zenject Binding |
| 24 | + |
| 25 | +Bind the `IActionUpdaterService` in your Zenject installer like this: |
| 26 | + |
| 27 | +```csharp |
| 28 | +public sealed class ServicesInstaller : MonoInstaller |
| 29 | +{ |
| 30 | + public override void InstallBindings() |
| 31 | + { |
| 32 | + Container.Bind<IActionUpdaterService>().To<ActionUpdaterService>().AsSingle(); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | +Don't forget to add to the ProjectContext: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +# Dispatcher |
| 42 | +Add the `ActionUpdateDispatcher` class to the ProjectContext prefab, Bootstrupper or any other object that will be the only one in the game. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +# Usage |
| 47 | + |
| 48 | +Inject IActionUpdaterService into your classes and subscribe to the desired update methods: |
| 49 | + |
| 50 | +```csharp |
| 51 | +public sealed class YourClass |
| 52 | +{ |
| 53 | + private IActionUpdaterService actionUpdater; |
| 54 | + |
| 55 | + [Inject] |
| 56 | + private void Constructor(IActionUpdaterService actionUpdater) => |
| 57 | + this.actionUpdater = actionUpdater; |
| 58 | + |
| 59 | + public void SomeMethod() |
| 60 | + { |
| 61 | + actionUpdater.Subscribe(MyUpdateMethod, UpdateType.Update); |
| 62 | + } |
| 63 | + |
| 64 | + private void MyUpdateMethod() |
| 65 | + { |
| 66 | + // Your update logic here |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Remember to unsubscribe from the service to prevent memory leaks: |
| 72 | + |
| 73 | +```csharp |
| 74 | +public void OnDestroy() |
| 75 | +{ |
| 76 | + actionUpdater.Unsubscribe(MyUpdateMethod, UpdateType.Update); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +# Pausing Updates |
| 81 | + |
| 82 | +You can pause and resume the execution of update methods: |
| 83 | + |
| 84 | +```csharp |
| 85 | +actionUpdater.SetPause(true); // Pauses updates |
| 86 | +actionUpdater.SetPause(false); // Resumes updates |
| 87 | +``` |
| 88 | + |
| 89 | +# Example |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +# License |
| 94 | + |
| 95 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 96 | + |
| 97 | +## Credits |
| 98 | + |
| 99 | +Developed by RimuruDev. |
0 commit comments