Skip to content

Commit d9b8e22

Browse files
committed
Update README
1 parent 029725f commit d9b8e22

File tree

1 file changed

+88
-189
lines changed

1 file changed

+88
-189
lines changed

README.md

Lines changed: 88 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -7,155 +7,24 @@
77

88
## Table of Contents
99

10-
* [GenConstructor](#1-genconstructor)
11-
* [MonoReadonly](#2-monoreadonly)
12-
* [PublicAccessor](#3-publicaccessor)
13-
* [Readonly](#4-readonly)
14-
* [Record](#5-record)
15-
* [Singleton](#6-singleton)
16-
* [Generated Code Examples](#generated-code-examples)
10+
* [GenConstructor](#genconstructor)
11+
* [MonoReadonly](#monoreadonly)
12+
* [PublicAccessor](#publicaccessor)
13+
* [Readonly](#readonly)
14+
* [Record](#record)
15+
* [Singleton](#singleton)
1716

1817
---
1918

20-
## Attribute Usage Guide
19+
## GenConstructor
2120

22-
### 1. `GenConstructor`
23-
24-
**Purpose:**
21+
**Description:**
2522
Automatically generates constructors for a class, including all non-static fields except those marked with `GenConstructorIgnore` .
2623

27-
**Usage Example:**
28-
29-
```csharp
30-
[GenConstructor]
31-
public partial class MyClass
32-
{
33-
private int _value1, _value2;
34-
public int value3;
35-
[GenConstructorIgnore] private int _ignored;
36-
private readonly int _readonlyValue;
37-
}
38-
```
39-
40-
This will generate a constructor accepting `_value1` , `_value2` , `value3` , and `_readonlyValue` .
41-
42-
---
43-
44-
### 2. `MonoReadonly`
45-
46-
**Purpose:**
47-
Restricts assignment to the field only within Unity MonoBehaviour lifecycle methods: `Awake` , `OnEnable` , `Start` , or `Reset` . Assigning elsewhere will trigger a Roslyn analyzer error.
48-
49-
**Usage Example:**
50-
51-
```csharp
52-
public class MyComponent : MonoBehaviour
53-
{
54-
[MonoReadonly]
55-
private int _score;
56-
57-
void Awake()
58-
{
59-
_score = 10; // Allowed
60-
}
61-
62-
void Update()
63-
{
64-
_score = 20; // Error: not allowed
65-
}
66-
}
67-
```
68-
69-
---
70-
71-
### 3. `PublicAccessor`
72-
73-
**Purpose:**
74-
Generates a public property accessor for a private or protected field.
75-
76-
**Usage Example:**
77-
78-
```csharp
79-
public partial class MyClass
80-
{
81-
[PublicAccessor] private int _value;
82-
}
83-
```
84-
85-
This will generate:
86-
87-
```csharp
88-
public int Value => _value;
89-
```
90-
91-
---
92-
93-
### 4. `Readonly`
94-
95-
**Purpose:**
96-
Prevents assignment to the field outside of its declaration (enforced by analyzer). Useful for enforcing immutability in Unity projects.
97-
98-
**Usage Example:**
99-
100-
```csharp
101-
public class MyClass
102-
{
103-
[Readonly]
104-
private int _score = 10;
105-
106-
void SomeMethod()
107-
{
108-
// _score = 20; // Error: assignment not allowed
109-
}
110-
}
111-
```
112-
113-
---
114-
115-
### 5. `Record`
116-
117-
**Purpose:**
118-
Generates a record-like class with value equality, `ToString` , and other utility methods, similar to C# 9 `record` but for older C# or Unity compatibility.
119-
120-
**Usage Example:**
121-
122-
```csharp
123-
[Record]
124-
public partial class MyRecord
125-
{
126-
public int Id;
127-
public string Name;
128-
}
129-
```
130-
131-
This will generate constructors, `Equals` , `GetHashCode` , and `ToString` for `MyRecord` .
132-
133-
---
134-
135-
### 6. `Singleton`
136-
137-
**Purpose:**
138-
Implements the Unity MonoBehaviour singleton pattern. The static `Instance` is set in the specified initialization method (e.g., `Awake` ).
139-
140-
**Usage Example:**
141-
142-
```csharp
143-
[Singleton("Awake")]
144-
public partial class GameManager : MonoBehaviour
145-
{
146-
// GameManager.Instance will be set in Awake()
147-
}
148-
```
149-
150-
---
24+
**How to use the attribute:**
25+
Apply `[GenConstructor]` to your partial class. All non-static fields (except those marked with `[GenConstructorIgnore]` ) will be included as parameters in the generated constructor.
15126

152-
## Generated Code Examples
153-
154-
Below you can see what code you write, what code is generated, and how to use the generated code for each attribute.
155-
156-
### 1. `GenConstructor`
157-
158-
**You write:**
27+
**Example (using the generated code):**
15928

16029
```csharp
16130
[GenConstructor]
@@ -166,9 +35,13 @@ public partial class MyClass
16635
[GenConstructorIgnore] private int _ignored;
16736
private readonly int _readonlyValue;
16837
}
38+
39+
// Usage
40+
var obj = new MyClass(1, 2, 3, 4);
16941
```
17042

171-
**Generated code:**
43+
<details>
44+
<summary>Generated code</summary>
17245

17346
```csharp
17447
public partial class MyClass
@@ -185,17 +58,19 @@ public partial class MyClass
18558
}
18659
```
18760

188-
**How to use:**
189-
190-
```csharp
191-
var obj = new MyClass(1, 2, 3, 4);
192-
```
61+
</details>
19362

19463
---
19564

196-
### 2. `MonoReadonly`
65+
## MonoReadonly
19766

198-
**You write:**
67+
**Description:**
68+
Restricts assignment to the field only within Unity MonoBehaviour lifecycle methods: `Awake` , `OnEnable` , `Start` , or `Reset` . Assigning elsewhere will trigger a Roslyn analyzer error.
69+
70+
**How to use the attribute:**
71+
Apply `[MonoReadonly]` to a field in a MonoBehaviour. Only assign to this field in `Awake` , `OnEnable` , `Start` , or `Reset` methods.
72+
73+
**Example (using the generated code):**
19974

20075
```csharp
20176
public class MyComponent : MonoBehaviour
@@ -208,65 +83,84 @@ public class MyComponent : MonoBehaviour
20883
}
20984
```
21085

211-
**Generated code:**
212-
_No code is generated, but a Roslyn analyzer will enforce assignment rules._
86+
<details>
87+
<summary>Generated code</summary>
21388

214-
**How to use:**
215-
Assign to the field only in `Awake` , `OnEnable` , `Start` , or `Reset` .
216-
Any assignment elsewhere will cause a compile-time error.
89+
_No code is generated, but a Roslyn analyzer will enforce assignment rules._
90+
</details>
21791

21892
---
21993

220-
### 3. `PublicAccessor`
94+
## PublicAccessor
22195

222-
**You write:**
96+
**Description:**
97+
Generates a public property accessor for a private or protected field.
98+
99+
**How to use the attribute:**
100+
Apply `[PublicAccessor]` to a private or protected field in a partial class. A public property will be generated for that field.
101+
102+
**Example (using the generated code):**
223103

224104
```csharp
225105
public partial class MyClass
226106
{
227107
[PublicAccessor] private int _value;
228108
}
109+
110+
// Usage
111+
var obj = new MyClass();
112+
int v = obj.Value; // Accesses the private _value field
229113
```
230114

231-
**Generated code:**
115+
<details>
116+
<summary>Generated code</summary>
232117

233118
```csharp
234119
public int Value => _value;
235120
```
236121

237-
**How to use:**
238-
239-
```csharp
240-
var obj = new MyClass();
241-
int v = obj.Value; // Accesses the private _value field
242-
```
122+
</details>
243123

244124
---
245125

246-
### 4. `Readonly`
126+
## Readonly
127+
128+
**Description:**
129+
Prevents assignment to the field outside of its declaration (enforced by analyzer). Useful for enforcing immutability in Unity projects.
247130

248-
**You write:**
131+
**How to use the attribute:**
132+
Apply `[Readonly]` to a field. You can only assign to this field at its declaration.
133+
134+
**Example (using the generated code):**
249135

250136
```csharp
251137
public class MyClass
252138
{
253139
[Readonly]
254140
private int _score = 10;
255141
}
142+
143+
// Usage
144+
// _score = 20; // Error: assignment not allowed
256145
```
257146

258-
**Generated code:**
259-
_No code is generated, but a Roslyn analyzer will prevent assignment outside of the declaration._
147+
<details>
148+
<summary>Generated code</summary>
260149

261-
**How to use:**
262-
You can only assign to `_score` at its declaration.
263-
Any assignment elsewhere will cause a compile-time error.
150+
_No code is generated, but a Roslyn analyzer will prevent assignment outside of the declaration._
151+
</details>
264152

265153
---
266154

267-
### 5. `Record`
155+
## Record
156+
157+
**Description:**
158+
Generates a record-like class with value equality, `ToString` , and other utility methods, similar to C# 9 `record` but for older C# or Unity compatibility.
268159

269-
**You write:**
160+
**How to use the attribute:**
161+
Apply `[Record]` to your partial class. The generator will create constructors, `Equals` , `GetHashCode` , and `ToString` methods.
162+
163+
**Example (using the generated code):**
270164

271165
```csharp
272166
[Record]
@@ -275,9 +169,14 @@ public partial class MyRecord
275169
public int Id;
276170
public string Name;
277171
}
172+
173+
// Usage
174+
var rec = new MyRecord(1, "Test");
175+
Console.WriteLine(rec); // MyRecord(Id: 1, Name: Test)
278176
```
279177

280-
**Generated code:**
178+
<details>
179+
<summary>Generated code</summary>
281180

282181
```csharp
283182
public partial class MyRecord
@@ -314,27 +213,32 @@ public partial class MyRecord
314213
}
315214
```
316215

317-
**How to use:**
318-
319-
```csharp
320-
var rec = new MyRecord(1, "Test");
321-
Console.WriteLine(rec); // MyRecord(Id: 1, Name: Test)
322-
```
216+
</details>
323217

324218
---
325219

326-
### 6. `Singleton`
220+
## Singleton
327221

328-
**You write:**
222+
**Description:**
223+
Implements the Unity MonoBehaviour singleton pattern. The static `Instance` is set in the specified initialization method (e.g., `Awake` ).
224+
225+
**How to use the attribute:**
226+
Apply `[Singleton("Awake")]` to your partial MonoBehaviour class. The generator will create a static `Instance` property and set it in the specified method.
227+
228+
**Example (using the generated code):**
329229

330230
```csharp
331231
[Singleton("Awake")]
332232
public partial class GameManager : MonoBehaviour
333233
{
334234
}
235+
236+
// Usage
237+
GameManager.Instance.DoSomething();
335238
```
336239

337-
**Generated code:**
240+
<details>
241+
<summary>Generated code</summary>
338242

339243
```csharp
340244
public partial class GameManager : MonoBehaviour
@@ -348,11 +252,6 @@ public partial class GameManager : MonoBehaviour
348252
}
349253
```
350254

351-
**How to use:**
352-
353-
```csharp
354-
// Anywhere in your code:
355-
GameManager.Instance.DoSomething();
356-
```
255+
</details>
357256

358257
---

0 commit comments

Comments
 (0)