|
1 | 1 | # ComponentCacheGenerator |
2 | 2 | A source generator that automatically generates a cache of components for Unity. |
| 3 | + |
| 4 | +ComponentCacheGenerator provides automatic generation of component caching code for Unity by generating code to cache `GetComponent<T>()` retrievals using a source generator, reducing the amount of code you need to write. |
| 5 | + |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +[](https://github.com/AnnulusGames/ComponentCacheGenerator/releases) |
| 9 | + |
| 10 | +[日本語版READMEはこちら](README_JA.md) |
| 11 | + |
| 12 | +## Setup |
| 13 | + |
| 14 | +### Requirements |
| 15 | + |
| 16 | +* Unity 2022.2 or later |
| 17 | + |
| 18 | +### Installation |
| 19 | + |
| 20 | +1. Open the Package Manager from Window > Package Manager. |
| 21 | +2. Click the "+" button > Add package from git URL. |
| 22 | +3. Enter the following URL: |
| 23 | + |
| 24 | +``` |
| 25 | +https://github.com/AnnulusGames/ComponentCacheGenerator.git?path=src/ComponentCacheGenerator/Assets/ComponentCacheGenerator |
| 26 | +``` |
| 27 | + |
| 28 | +Alternatively, open Packages/manifest.json and add the following to the dependencies block: |
| 29 | + |
| 30 | +```json |
| 31 | +{ |
| 32 | + "dependencies": { |
| 33 | + "com.annulusgames.enhanced-on-screen-stick": "https://github.com/AnnulusGames/ComponentCacheGenerator.git?path=src/ComponentCacheGenerator/Assets/ComponentCacheGenerator" |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Basic Usage |
| 39 | + |
| 40 | +In Unity, retrieving components with `GetComponent<T>()` is known to be costly, so it's common practice to cache references beforehand in `Awake()` or `Start()`. Additionally, `[RequireComponent(typeof(T))]` is often used to prevent forgetting to add components. While these practices are necessary for performance and maintainability, they can lead to increased code verbosity as the number of components grows. |
| 41 | + |
| 42 | +```cs |
| 43 | +using UnityEngine; |
| 44 | + |
| 45 | +[RequireComponent(typeof(FooComponent))] |
| 46 | +[RequireComponent(typeof(BarComponent))] |
| 47 | +[RequireComponent(typeof(BazComponent))] |
| 48 | +public class SomeBehaviour : MonoBehaviour |
| 49 | +{ |
| 50 | + FooComponent fooComponent; |
| 51 | + BarComponent barComponent; |
| 52 | + BazComponent bazComponent; |
| 53 | + |
| 54 | + void Awake() |
| 55 | + { |
| 56 | + fooComponent = GetComponent<FooComponent>(); |
| 57 | + barComponent = GetComponent<BarComponent>(); |
| 58 | + bazComponent = GetComponent<BazComponent>(); |
| 59 | + } |
| 60 | + |
| 61 | + void Update() |
| 62 | + { |
| 63 | + fooComponent.Foo(); |
| 64 | + barComponent.Bar(); |
| 65 | + bazComponent.Baz(); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +ComponentCacheGenerator automatically generates this code using the `[GenerateComponentCache]` attribute. |
| 71 | + |
| 72 | +```cs |
| 73 | +using UnityEngine; |
| 74 | +using ComponentCacheGenerator; |
| 75 | + |
| 76 | +[GenerateComponentCache(typeof(FooComponent))] |
| 77 | +[GenerateComponentCache(typeof(BarComponent))] |
| 78 | +[GenerateComponentCache(typeof(BazComponent))] |
| 79 | +public partial class SomeBehaviour : MonoBehaviour |
| 80 | +{ |
| 81 | + void Update() |
| 82 | + { |
| 83 | + fooComponent.Foo(); |
| 84 | + barComponent.Bar(); |
| 85 | + bazComponent.Baz(); |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +ComponentCacheGenerator generates properties to cache each component and a `CacheComponents()` method. If the target class does not have an `Awake()` method, one will be automatically added to the class. (Note: If the target class already has an `Awake()` method, you need to manually call `CacheComponents()`.) |
| 91 | + |
| 92 | +```cs |
| 93 | +// Automatically added if Awake does not exist |
| 94 | +void Awake() |
| 95 | +{ |
| 96 | + CacheComponents() |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Generation Options |
| 101 | + |
| 102 | +You can specify generation code settings by specifying values for properties in the `[GenerateComponentCache]` attribute. |
| 103 | + |
| 104 | +| Property | Description | |
| 105 | +| - | - | |
| 106 | +| SearchScope | Specifies the scope to search for components. If multiple are specified, the search will be performed in the order of Self > Children > Parent. (Default is Self) | |
| 107 | +| IsRequired | If IsRequired is true, an exception will be thrown if the component is not found when `CacheComponents()` is called. If the search scope is Self, `[RequireComponent]` attribute will be automatically generated. (Default is true) | |
| 108 | +| PropertyName | Specifies the name of the generated property. If not specified, the lower camel case version of the target component's class name will be used. | |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +[MIT License](LICENSE) |
0 commit comments