You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Automatically generates constructors for a class, including all non-static fields except those marked with `GenConstructorIgnore` .
26
23
27
-
**Usage Example:**
28
-
29
-
```csharp
30
-
[GenConstructor]
31
-
publicpartialclassMyClass
32
-
{
33
-
privateint_value1, _value2;
34
-
publicintvalue3;
35
-
[GenConstructorIgnore] privateint_ignored;
36
-
privatereadonlyint_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
-
publicclassMyComponent : MonoBehaviour
53
-
{
54
-
[MonoReadonly]
55
-
privateint_score;
56
-
57
-
voidAwake()
58
-
{
59
-
_score=10; // Allowed
60
-
}
61
-
62
-
voidUpdate()
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
-
publicpartialclassMyClass
80
-
{
81
-
[PublicAccessor] privateint_value;
82
-
}
83
-
```
84
-
85
-
This will generate:
86
-
87
-
```csharp
88
-
publicintValue=>_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
-
publicclassMyClass
102
-
{
103
-
[Readonly]
104
-
privateint_score=10;
105
-
106
-
voidSomeMethod()
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
-
publicpartialclassMyRecord
125
-
{
126
-
publicintId;
127
-
publicstringName;
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
-
publicpartialclassGameManager : 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.
151
26
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):**
159
28
160
29
```csharp
161
30
[GenConstructor]
@@ -166,9 +35,13 @@ public partial class MyClass
166
35
[GenConstructorIgnore] privateint_ignored;
167
36
privatereadonlyint_readonlyValue;
168
37
}
38
+
39
+
// Usage
40
+
varobj=newMyClass(1, 2, 3, 4);
169
41
```
170
42
171
-
**Generated code:**
43
+
<details>
44
+
<summary>Generated code</summary>
172
45
173
46
```csharp
174
47
publicpartialclassMyClass
@@ -185,17 +58,19 @@ public partial class MyClass
185
58
}
186
59
```
187
60
188
-
**How to use:**
189
-
190
-
```csharp
191
-
varobj=newMyClass(1, 2, 3, 4);
192
-
```
61
+
</details>
193
62
194
63
---
195
64
196
-
### 2. `MonoReadonly`
65
+
##MonoReadonly
197
66
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):**
199
74
200
75
```csharp
201
76
publicclassMyComponent : MonoBehaviour
@@ -208,65 +83,84 @@ public class MyComponent : MonoBehaviour
208
83
}
209
84
```
210
85
211
-
**Generated code:**
212
-
_No code is generated, but a Roslyn analyzer will enforce assignment rules._
86
+
<details>
87
+
<summary>Generated code</summary>
213
88
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>
217
91
218
92
---
219
93
220
-
### 3. `PublicAccessor`
94
+
##PublicAccessor
221
95
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):**
223
103
224
104
```csharp
225
105
publicpartialclassMyClass
226
106
{
227
107
[PublicAccessor] privateint_value;
228
108
}
109
+
110
+
// Usage
111
+
varobj=newMyClass();
112
+
intv=obj.Value; // Accesses the private _value field
229
113
```
230
114
231
-
**Generated code:**
115
+
<details>
116
+
<summary>Generated code</summary>
232
117
233
118
```csharp
234
119
publicintValue=>_value;
235
120
```
236
121
237
-
**How to use:**
238
-
239
-
```csharp
240
-
varobj=newMyClass();
241
-
intv=obj.Value; // Accesses the private _value field
242
-
```
122
+
</details>
243
123
244
124
---
245
125
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.
247
130
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):**
249
135
250
136
```csharp
251
137
publicclassMyClass
252
138
{
253
139
[Readonly]
254
140
privateint_score=10;
255
141
}
142
+
143
+
// Usage
144
+
// _score = 20; // Error: assignment not allowed
256
145
```
257
146
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>
260
149
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>
264
152
265
153
---
266
154
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.
268
159
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):**
270
164
271
165
```csharp
272
166
[Record]
@@ -275,9 +169,14 @@ public partial class MyRecord
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):**
329
229
330
230
```csharp
331
231
[Singleton("Awake")]
332
232
publicpartialclassGameManager : MonoBehaviour
333
233
{
334
234
}
235
+
236
+
// Usage
237
+
GameManager.Instance.DoSomething();
335
238
```
336
239
337
-
**Generated code:**
240
+
<details>
241
+
<summary>Generated code</summary>
338
242
339
243
```csharp
340
244
publicpartialclassGameManager : MonoBehaviour
@@ -348,11 +252,6 @@ public partial class GameManager : MonoBehaviour
0 commit comments