@@ -21,11 +21,16 @@ As you already know, Roslyn's source generator is too sophisticated. This framew
21
21
<p ><details lang =" en " --open ><summary >Table of Contents</summary >
22
22
23
23
- [ Sample Code] ( #sample-code )
24
- - [ Result] ( #result )
25
- - [ Output Path] ( #output-path )
24
+ - [ Method Generator] ( #method-generator )
25
+ - [ How to Use] ( #how-to-use )
26
+ - [ Result] ( #result )
27
+ - [ Generic Generator] ( #generic-generator )
28
+ - [ Result] ( #result-1 )
29
+ - [ Output Directory and File Name] ( #output-directory-and-file-name )
26
30
- [ Utility Functions for Build] ( #utility-functions-for-build )
27
31
- [ Technical Notes] ( #technical-notes )
28
32
- [ Naming Convention] ( #naming-convention )
33
+ - [ ` <auto-generated/> ` Tag] ( #auto-generated-tag )
29
34
- [ Installation] ( #installation )
30
35
- [ Editor Extensions] ( #editor-extensions )
31
36
- [ Copyright] ( #copyright )
@@ -35,9 +40,12 @@ As you already know, Roslyn's source generator is too sophisticated. This framew
35
40
<!-- ----- End of Details EN Tag -------> </details ></p >
36
41
37
42
43
+
44
+
45
+
38
46
# Sample Code
39
47
40
- Minimal implementation of source generator here .
48
+ Minimal implementation codes of source generator.
41
49
42
50
<p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
43
51
@@ -46,14 +54,97 @@ Minimal implementation of source generator here.
46
54
<!-- ----- End of Details JA Tag -------> </details ></p >
47
55
48
56
57
+ ## Method Generator
58
+
59
+ This example will add ` Pacic() ` method to target class.
60
+
61
+ <p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
62
+
63
+ ターゲットのクラスに ` Panic() ` メソッドを追加するサンプル。
64
+
65
+ <!-- ----- End of Details JA Tag -------> </details ></p >
66
+
67
+
68
+ ``` csharp
69
+ public class PanicMethodGenerator
70
+ {
71
+ static string OutputFileName () => " PanicMethod.cs" ; // -> PanicMethod.<TargetClass>.<GeneratorClass>.g.cs
72
+
73
+ static bool Emit (USGContext context , StringBuilder sb )
74
+ {
75
+ if (! context .TargetClass .IsClass || context .TargetClass .IsAbstract )
76
+ return false ; // return false to tell USG doesn't write file.
77
+
78
+ // code generation
79
+ sb .Append ($@"
80
+ namespace {context .TargetClass .Namespace }
81
+ {{
82
+ internal partial class {context .TargetClass .Name }
83
+ {{
84
+ public void Panic() => throw new System.Exception();
85
+ }}
86
+ }}
87
+ " );
88
+ return true ;
89
+ }
90
+ }
91
+ ```
92
+
93
+
94
+
95
+ ### How to Use
96
+
97
+
98
+ ``` csharp
99
+ using SatorImaging .UnitySourceGenerator ;
100
+
101
+ namespace Sample
102
+ {
103
+ // Add attribute to target class to use method generator.
104
+ // Note that class must be defined as partial class.
105
+ [UnitySourceGenerator (typeof (PanicMethodGenerator ), OverwriteIfFileExists = false )]
106
+ internal partial class MethodGeneratorSample
107
+ {
108
+ }
109
+
110
+ }
111
+ ```
112
+
113
+ ### Result
114
+
115
+ Generated code looks like this.
116
+
117
+ ``` csharp
118
+ // <auto-generated>PanicMethodGenerator</auto-generated>
119
+
120
+ namespace Sample
121
+ {
122
+ internal partial class MethodGeneratorSample
123
+ {
124
+ public void Panic () => throw new System .Exception ();
125
+ }
126
+ }
127
+ ```
128
+
129
+
130
+
131
+ ## Generic Generator
132
+
133
+ Here is target-less generator example.
134
+
135
+ It is useful to generate static database that cannot be generated on Unity runtime. For example, asset GUIDs database, resource integrity tables, etc.
136
+
137
+
138
+
139
+
49
140
``` csharp
50
141
using System .Text ;
51
142
using SatorImaging .UnitySourceGenerator ;
52
143
53
144
[UnitySourceGenerator (OverwriteIfFileExists = false )]
54
145
class MinimalGenerator
55
146
{
56
- static string OutputFileName () => " Test.cs" ; // USG automatically update to -> Test.<ClassName>.g.cs
147
+ static string OutputFileName () => " Test.cs" ; // -> Test.<ClassName>.g.cs
57
148
58
149
static bool Emit (USGContext context , StringBuilder sb )
59
150
{
@@ -73,43 +164,36 @@ class MinimalGenerator
73
164
74
165
75
166
76
- ## Result
77
-
78
- USG automatically adds document tag at the beginning of generated file. You can remove this document tag by ` sb.Clear() ` in ` Emit() ` method.
167
+ ### Result
79
168
80
169
81
- <p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
82
-
83
- 書き出される内容は以下の通り。渡される ` StringBuilder ` の冒頭にはドキュメントタグが入ってます。
84
-
85
- <!-- ----- End of Details JA Tag -------> </details ></p >
86
-
87
170
``` csharp
88
171
// <auto-generated>MinimalGenerator</auto-generated>
89
172
Asset Path : Assets / Scripts / MinimalGenerator .cs
90
173
Hello World from Sample .MinimalGenerator
91
174
```
92
175
93
176
177
+ # Output Directory and File Name
94
178
95
- ## Output Path
179
+ Source Generator creates ` USG.g ` folder next to target script and append class names to file name.
96
180
97
- USG creates ` USG.g ` folder next to generator script file. Resulting file path will be:
181
+ Resulting file path will be:
98
182
99
183
- Assets/Scripts/<b >USG.g</b >/Test<b >.MinimalGenerator.g</b >.cs
184
+ - Assets/Scripts/<b >USG.g</b >/PanicMethod<b >.MethodGeneratorSample.PanicMethodGenerator.g</b >.cs
100
185
101
186
> NOTE: In above example, output path is modified so that resulting file name is ` Test.MinimalGenerator.g.cs_MyFirstTest.txt `
102
187
103
188
104
189
<p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
105
190
106
- 書き出し先は上記の通り。フォルダーとジェネレータークラス名が付与されます 。
191
+ 書き出し先は上記の通り。フォルダーとターゲット・ジェネレータークラス名が付与されます 。
107
192
108
193
<!-- ----- End of Details JA Tag -------> </details ></p >
109
194
110
195
111
196
112
-
113
197
# Utility Functions for Build
114
198
115
199
There are utility functions to perform source code generation on build event.
@@ -172,6 +256,19 @@ As of C# 9.0, it doesn't allow to define `abstract static` methods in interface,
172
256
173
257
174
258
259
+ ## ` <auto-generated/> ` Tag
260
+
261
+ USG automatically adds document tag at the beginning of generated file. You can remove this document tag by ` sb.Clear() ` in ` Emit() ` method.
262
+
263
+
264
+ <p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
265
+
266
+ 渡される ` StringBuilder ` の冒頭にはドキュメントタグが入ってます。不要なら ` sb.Clear() ` してください。
267
+
268
+ <!-- ----- End of Details JA Tag -------> </details ></p >
269
+
270
+
271
+
175
272
# Installation
176
273
177
274
Use the following git URL in Unity Package Manager (UPM).
@@ -187,14 +284,15 @@ Use the following git URL in Unity Package Manager (UPM).
187
284
188
285
手動でソースコード生成イベントの発火も可能です。「ジェネレーターのスクリプトファイル」か「生成されたファイル」を選択して、Project ウインドウで ` Reimport ` か ` Unity Source Generator ` 以下のメニューを実行します。
189
286
190
- ` Force Generate... ` はクラスアトリビュートの設定に関わらず強制的に上書き生成します。
287
+ ジェネレーターとして参照されているファイルを Reimport した場合は、関連するクラスすべてが再生成されます。 ` Force Generate... ` はクラスアトリビュートの設定に関わらず強制的に上書き生成します。
191
288
192
289
<!-- ----- End of Details JA Tag -------> </details ></p >
193
290
194
291
There is an ability to invoke source code generation by hand. With generator script file or generated file selected in Project window:
195
292
196
293
- ` Reimport `
197
294
- This command respects ` OverwriteIfFileExists ` setting by generator class attribute.
295
+ - Classes referencing selected generator will also be re-generated.
198
296
199
297
- ` Unity Source Generator > Force Generate `
200
298
- This command will force re-generate source code even if overwrite setting is disabled.
@@ -250,6 +348,8 @@ SOFTWARE.
250
348
251
349
252
350
351
+
352
+
253
353
  ;
254
354
  ;
255
355
0 commit comments