@@ -21,11 +21,16 @@ As you already know, Roslyn's source generator is too sophisticated. This framew
2121<p ><details lang =" en " --open ><summary >Table of Contents</summary >
2222
2323- [ 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 )
2630- [ Utility Functions for Build] ( #utility-functions-for-build )
2731- [ Technical Notes] ( #technical-notes )
2832 - [ Naming Convention] ( #naming-convention )
33+ - [ ` <auto-generated/> ` Tag] ( #auto-generated-tag )
2934- [ Installation] ( #installation )
3035- [ Editor Extensions] ( #editor-extensions )
3136- [ Copyright] ( #copyright )
@@ -35,9 +40,12 @@ As you already know, Roslyn's source generator is too sophisticated. This framew
3540<!-- ----- End of Details EN Tag -------> </details ></p >
3641
3742
43+
44+
45+
3846# Sample Code
3947
40- Minimal implementation of source generator here .
48+ Minimal implementation codes of source generator.
4149
4250<p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
4351
@@ -46,14 +54,97 @@ Minimal implementation of source generator here.
4654<!-- ----- End of Details JA Tag -------> </details ></p >
4755
4856
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+
49140``` csharp
50141using System .Text ;
51142using SatorImaging .UnitySourceGenerator ;
52143
53144[UnitySourceGenerator (OverwriteIfFileExists = false )]
54145class MinimalGenerator
55146{
56- static string OutputFileName () => " Test.cs" ; // USG automatically update to -> Test.<ClassName>.g.cs
147+ static string OutputFileName () => " Test.cs" ; // -> Test.<ClassName>.g.cs
57148
58149 static bool Emit (USGContext context , StringBuilder sb )
59150 {
@@ -73,43 +164,36 @@ class MinimalGenerator
73164
74165
75166
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
79168
80169
81- <p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
82-
83- 書き出される内容は以下の通り。渡される ` StringBuilder ` の冒頭にはドキュメントタグが入ってます。
84-
85- <!-- ----- End of Details JA Tag -------> </details ></p >
86-
87170``` csharp
88171// <auto-generated>MinimalGenerator</auto-generated>
89172Asset Path : Assets / Scripts / MinimalGenerator .cs
90173Hello World from Sample .MinimalGenerator
91174```
92175
93176
177+ # Output Directory and File Name
94178
95- ## Output Path
179+ Source Generator creates ` USG.g ` folder next to target script and append class names to file name.
96180
97- USG creates ` USG.g ` folder next to generator script file. Resulting file path will be:
181+ Resulting file path will be:
98182
99183- 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
100185
101186> NOTE: In above example, output path is modified so that resulting file name is ` Test.MinimalGenerator.g.cs_MyFirstTest.txt `
102187
103188
104189<p ><details lang =" ja " --open ><summary ><small >日本語 / JA</small ></summary >
105190
106- 書き出し先は上記の通り。フォルダーとジェネレータークラス名が付与されます 。
191+ 書き出し先は上記の通り。フォルダーとターゲット・ジェネレータークラス名が付与されます 。
107192
108193<!-- ----- End of Details JA Tag -------> </details ></p >
109194
110195
111196
112-
113197# Utility Functions for Build
114198
115199There 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,
172256
173257
174258
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+
175272# Installation
176273
177274Use the following git URL in Unity Package Manager (UPM).
@@ -187,14 +284,15 @@ Use the following git URL in Unity Package Manager (UPM).
187284
188285手動でソースコード生成イベントの発火も可能です。「ジェネレーターのスクリプトファイル」か「生成されたファイル」を選択して、Project ウインドウで ` Reimport ` か ` Unity Source Generator ` 以下のメニューを実行します。
189286
190- ` Force Generate... ` はクラスアトリビュートの設定に関わらず強制的に上書き生成します。
287+ ジェネレーターとして参照されているファイルを Reimport した場合は、関連するクラスすべてが再生成されます。 ` Force Generate... ` はクラスアトリビュートの設定に関わらず強制的に上書き生成します。
191288
192289<!-- ----- End of Details JA Tag -------> </details ></p >
193290
194291There is an ability to invoke source code generation by hand. With generator script file or generated file selected in Project window:
195292
196293- ` Reimport `
197294 - This command respects ` OverwriteIfFileExists ` setting by generator class attribute.
295+ - Classes referencing selected generator will also be re-generated.
198296
199297- ` Unity Source Generator > Force Generate `
200298 - This command will force re-generate source code even if overwrite setting is disabled.
@@ -250,6 +348,8 @@ SOFTWARE.
250348
251349
252350
351+
352+
253353  ;
254354  ;
255355
0 commit comments