Skip to content

Commit 53c3a52

Browse files
committed
Update: README
1 parent 4d447d9 commit 53c3a52

2 files changed

Lines changed: 222 additions & 0 deletions

File tree

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,112 @@
11
# ComponentCacheGenerator
22
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](https://img.shields.io/badge/LICENSE-MIT-green.svg)](LICENSE)
7+
![unity-version](https://img.shields.io/badge/unity-2022.2+-000.svg)
8+
[![releases](https://img.shields.io/github/release/AnnulusGames/ComponentCacheGenerator.svg)](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)

README_JA.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# ComponentCacheGenerator
2+
A source generator that automatically generates a cache of components for Unity.
3+
4+
ComponentCacheGeneratorはUnity用に作成されたComponentのキャッシュ処理を自動生成を提供するSource Generatorです。`GetComponent<T>()`の取得をキャッシュするコードをSource Generatorで生成することでコードの記述量を削減します。
5+
6+
[![license](https://img.shields.io/badge/LICENSE-MIT-green.svg)](LICENSE)
7+
![unity-version](https://img.shields.io/badge/unity-2022.2+-000.svg)
8+
[![releases](https://img.shields.io/github/release/AnnulusGames/ComponentCacheGenerator.svg)](https://github.com/AnnulusGames/ComponentCacheGenerator/releases)
9+
10+
[English README is here.](README.md)
11+
12+
## セットアップ
13+
14+
### 要件
15+
16+
* Unity 2022.2 以上
17+
18+
### インストール
19+
20+
1. Window > Package ManagerからPackage Managerを開く
21+
2. 「+」ボタン > Add package from git URL
22+
3. 以下のURLを入力する
23+
24+
```
25+
https://github.com/AnnulusGames/ComponentCacheGenerator.git?path=src/ComponentCacheGenerator/Assets/ComponentCacheGenerator
26+
```
27+
28+
またはPackages/manifest.jsonを開き、dependenciesブロックに以下を追記
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+
## 基本的な使い方
39+
40+
Unityにおいて`GetComponent<T>()`でコンポーネントを取得する処理は高コストであることがよく知られており、使用する際は`Awake()``Start()`で参照をあらかじめキャッシュしておくことが一般的です。また、各コンポーネントの追加忘れを防止するために`[RequireComponent(typeof(T))]`を併用することも多くなります。これらはパフォーマンスや保守性の向上のために必要ですが、コンポーネントが増えていくとコードの記述量が増加していきます。
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は`[GenerateComponentCache]`属性を使用してこれらのコードを自動生成します。
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は各コンポーネントをキャッシュするプロパティと`CacheComponents()`メソッドを生成します。また、対象が`Awake()`メソッドを持たない場合はクラス内に自動で追加されます。(対象のクラスが既に`Awake()`を持っている場合は手動で`CacheComponents()`を呼ぶ必要があることに注意してください。)
91+
92+
```cs
93+
// Awakeが存在しない場合は自動で追加される
94+
void Awake()
95+
{
96+
CacheComponents()
97+
}
98+
```
99+
100+
## 生成オプション
101+
102+
`[GenerateComponentCache]`属性のプロパティの値を指定することで、生成コードの設定を行うことが可能です。
103+
104+
| プロパティ | 説明 |
105+
| - | - |
106+
| SearchScope | コンポーネントを探索する範囲を指定します。複数指定された場合はSelf > Children > Parentの順番で探索を行います。(デフォルトはSelf) |
107+
| IsRequired | IsRequiredがtrueの場合、`CacheComponents()`でコンポーネントが見つからなかったときに例外をスローします。また、探索範囲がSelfの場合は`[RequireComponent]`属性を自動で生成します。(デフォルトはtrue) |
108+
| PropertyName | 生成するプロパティの名前を指定します。指定がない場合には対象のコンポーネントのクラス名をlower camel caseに変換した名前が使用されます。 |
109+
110+
## ライセンス
111+
112+
[MIT License](LICENSE)

0 commit comments

Comments
 (0)