0
# Unity Asset Classes and Objects
1
2
Complete collection of Unity asset types including textures, audio, meshes, shaders, and game objects. Each class provides typed access to Unity's native data structures with methods for reading, modifying, and exporting assets.
3
4
## Capabilities
5
6
### ObjectReader Class
7
8
Foundation class that provides access to Unity objects with core functionality for reading and saving asset data. This is the interface used to access objects from loaded asset files.
9
10
```python { .api }
11
class ObjectReader:
12
"""
13
Reader interface for Unity objects providing typed access to asset data.
14
"""
15
16
@property
17
def type(self) -> ClassIDType:
18
"""
19
Get the ClassIDType of this object.
20
21
Returns:
22
ClassIDType: The Unity class type identifier
23
"""
24
25
@property
26
def path_id(self) -> int:
27
"""
28
Get the unique path ID of this object within its asset file.
29
30
Returns:
31
int: Object path ID
32
"""
33
34
@property
35
def class_id(self) -> int:
36
"""
37
Get the numeric class ID.
38
39
Returns:
40
int: Unity class ID number
41
"""
42
43
def read(self, check_read: bool = True) -> T:
44
"""
45
Read object data as a parsed Unity class instance.
46
47
Parameters:
48
- check_read: Whether to validate read operation
49
50
Returns:
51
T: Parsed object instance with typed properties
52
"""
53
54
def read_typetree(self, nodes: Optional[NodeInput] = None, wrap: bool = False, check_read: bool = True) -> Union[dict, T]:
55
"""
56
Read object data using typetree system.
57
58
Parameters:
59
- nodes: Optional type tree nodes
60
- wrap: Whether to wrap result in object class
61
- check_read: Whether to validate read operation
62
63
Returns:
64
Union[dict, T]: Raw dictionary or wrapped object instance
65
"""
66
67
def save_typetree(self, tree: Union[dict, T], nodes: Optional[NodeInput] = None, writer: Optional[EndianBinaryWriter] = None):
68
"""
69
Save modified object data from dictionary or object instance.
70
71
Parameters:
72
- tree: Modified object data as dict or object instance
73
- nodes: Optional type tree nodes
74
- writer: Optional binary writer
75
"""
76
77
def get_raw_data(self) -> bytes:
78
"""
79
Get raw binary data of the object.
80
81
Returns:
82
bytes: Raw object data
83
"""
84
85
def set_raw_data(self, data: bytes):
86
"""
87
Set raw binary data for the object.
88
89
Parameters:
90
- data: New raw object data
91
"""
92
```
93
94
### Base Object Class
95
96
Base class for all parsed Unity asset instances that provides common functionality.
97
98
```python { .api }
99
class Object:
100
"""
101
Base class for all Unity asset instances.
102
"""
103
104
object_reader: Optional[ObjectReader]
105
106
def save(self) -> None:
107
"""
108
Save this object's changes back to the asset file.
109
Calls save_typetree on the associated ObjectReader.
110
"""
111
```
112
113
### Texture Assets
114
115
Classes for working with Unity texture assets including 2D textures and texture arrays.
116
117
```python { .api }
118
class Texture2D(Object):
119
"""
120
Unity 2D texture asset with format conversion and export capabilities.
121
"""
122
123
m_Name: str
124
m_Width: int
125
m_Height: int
126
m_TextureFormat: int
127
m_CompleteImageSize: int
128
m_IsReadable: bool
129
image_data: bytes
130
m_StreamData: Optional[StreamingInfo]
131
m_MipMap: Optional[bool]
132
m_MipCount: Optional[int]
133
134
@property
135
def image(self) -> Image.Image:
136
"""
137
Get texture as PIL Image with automatic format conversion.
138
139
Returns:
140
PIL.Image.Image: Decoded image data ready for manipulation
141
"""
142
143
@image.setter
144
def image(self, img: Union[Image.Image, str, BinaryIO]):
145
"""
146
Set texture from PIL Image, file path, or file-like object.
147
148
Parameters:
149
- img: PIL Image, file path string, or file-like object
150
"""
151
152
def set_image(self, img: Union[Image.Image, str, BinaryIO], target_format: Optional[int] = None, mipmap_count: int = 1):
153
"""
154
Set texture image with format and mipmap control.
155
156
Parameters:
157
- img: PIL Image, file path, or file-like object
158
- target_format: Target texture format (uses current if None)
159
- mipmap_count: Number of mipmap levels to generate
160
"""
161
162
def get_image_data(self) -> bytes:
163
"""
164
Get raw image data bytes, loading from stream if necessary.
165
166
Returns:
167
bytes: Raw texture data in Unity format
168
"""
169
170
class Texture2DArray:
171
"""
172
Unity 2D texture array for advanced rendering techniques.
173
"""
174
175
@property
176
def name(self):
177
"""Texture array name."""
178
179
@property
180
def m_Width(self):
181
"""Texture width in pixels."""
182
183
@property
184
def m_Height(self):
185
"""Texture height in pixels."""
186
187
@property
188
def m_Depth(self):
189
"""Number of textures in the array."""
190
191
@property
192
def m_TextureFormat(self):
193
"""Pixel format of textures in the array."""
194
```
195
196
### Audio Assets
197
198
Classes for Unity audio clips with format conversion and export functionality.
199
200
```python { .api }
201
class AudioClip(Object):
202
"""
203
Unity audio clip with conversion and export capabilities.
204
"""
205
206
m_Name: str
207
m_Length: Optional[float]
208
m_Frequency: Optional[int]
209
m_Channels: Optional[int]
210
m_BitsPerSample: Optional[int]
211
m_CompressionFormat: Optional[int]
212
m_LoadType: Optional[int]
213
m_3D: Optional[bool]
214
m_AudioData: Optional[List[int]]
215
m_Resource: Optional[StreamedResource]
216
217
@property
218
def samples(self) -> Dict[str, bytes]:
219
"""
220
Get decoded audio samples as WAV data.
221
222
Returns:
223
Dict[str, bytes]: Dictionary mapping sample names to WAV file bytes
224
"""
225
226
@property
227
def extension(self) -> str:
228
"""
229
Get file extension based on compression format.
230
231
Returns:
232
str: File extension (.wav, .ogg, .mp3, etc.)
233
"""
234
```
235
236
### 3D Mesh Assets
237
238
Classes for Unity mesh data including vertices, triangles, and material information.
239
240
```python { .api }
241
class Mesh:
242
"""
243
Unity mesh asset containing 3D geometry data.
244
"""
245
246
@property
247
def name(self):
248
"""Mesh name."""
249
250
@property
251
def vertices(self):
252
"""
253
Vertex positions.
254
255
Returns:
256
List[Vector3f]: 3D vertex coordinates
257
"""
258
259
@property
260
def triangles(self):
261
"""
262
Triangle indices.
263
264
Returns:
265
List[int]: Vertex indices forming triangles
266
"""
267
268
@property
269
def normals(self):
270
"""
271
Vertex normals.
272
273
Returns:
274
List[Vector3f]: Normal vectors for lighting
275
"""
276
277
@property
278
def uv(self):
279
"""
280
UV texture coordinates.
281
282
Returns:
283
List[Vector2f]: Texture mapping coordinates
284
"""
285
286
@property
287
def colors(self):
288
"""
289
Vertex colors.
290
291
Returns:
292
List[ColorRGBA32]: Per-vertex color data
293
"""
294
```
295
296
### Sprite Assets
297
298
Classes for Unity 2D sprite assets with texture references and metadata.
299
300
```python { .api }
301
class Sprite:
302
"""
303
Unity sprite asset for 2D graphics.
304
"""
305
306
@property
307
def name(self):
308
"""Sprite name."""
309
310
@property
311
def texture(self):
312
"""
313
Associated texture asset.
314
315
Returns:
316
PPtr[Texture2D]: Reference to texture
317
"""
318
319
@property
320
def textureRect(self):
321
"""
322
Rectangle defining sprite area in texture.
323
324
Returns:
325
Rectf: Sprite bounds in texture coordinates
326
"""
327
328
@property
329
def pivot(self):
330
"""
331
Sprite pivot point.
332
333
Returns:
334
Vector2f: Pivot coordinates
335
"""
336
```
337
338
### Game Object Hierarchy
339
340
Classes for Unity game objects and scene hierarchy.
341
342
```python { .api }
343
class GameObject:
344
"""
345
Unity game object - container for components in scenes.
346
"""
347
348
@property
349
def name(self):
350
"""Game object name."""
351
352
@property
353
def m_Component(self):
354
"""
355
List of attached components.
356
357
Returns:
358
List[ComponentPair]: Component references
359
"""
360
361
@property
362
def m_Layer(self):
363
"""
364
Rendering layer.
365
366
Returns:
367
int: Layer index
368
"""
369
370
@property
371
def m_IsActive(self):
372
"""
373
Whether the game object is active.
374
375
Returns:
376
bool: Active state
377
"""
378
```
379
380
### Rendering Components
381
382
Classes for Unity rendering components and materials.
383
384
```python { .api }
385
class Renderer:
386
"""
387
Base class for Unity renderer components.
388
"""
389
390
@property
391
def m_Materials(self):
392
"""
393
List of materials used by this renderer.
394
395
Returns:
396
List[PPtr[Material]]: Material references
397
"""
398
399
@property
400
def m_Enabled(self):
401
"""
402
Whether the renderer is enabled.
403
404
Returns:
405
bool: Enabled state
406
"""
407
```
408
409
### Shader Assets
410
411
Classes for Unity shader assets and GPU programs.
412
413
```python { .api }
414
class Shader:
415
"""
416
Unity shader asset containing GPU programs.
417
"""
418
419
@property
420
def name(self):
421
"""Shader name."""
422
423
@property
424
def m_ParsedForm(self):
425
"""
426
Parsed shader data.
427
428
Returns:
429
SerializedShader: Structured shader information
430
"""
431
```
432
433
### Pointer References
434
435
Class for managing references between Unity objects across files.
436
437
```python { .api }
438
class PPtr:
439
"""
440
Smart pointer for referencing Unity objects.
441
"""
442
443
@property
444
def file_id(self):
445
"""
446
Source file identifier.
447
448
Returns:
449
int: File ID containing the referenced object
450
"""
451
452
@property
453
def path_id(self):
454
"""
455
Object identifier within file.
456
457
Returns:
458
int: Unique object ID
459
"""
460
461
def resolve(self):
462
"""
463
Resolve pointer to actual object.
464
465
Returns:
466
Object: Referenced Unity object
467
"""
468
```
469
470
## Usage Examples
471
472
### Working with Textures
473
474
```python
475
import UnityPy
476
477
env = UnityPy.load("texture_assets/")
478
479
for obj in env.objects:
480
if obj.type.name == "Texture2D":
481
texture = obj.read()
482
483
print(f"Texture: {texture.name}")
484
print(f"Size: {texture.m_Width}x{texture.m_Height}")
485
print(f"Format: {texture.m_TextureFormat}")
486
487
# Export as PNG
488
texture.save(f"exports/{texture.name}.png")
489
490
# Get as PIL Image for processing
491
image = texture.image
492
resized = image.resize((256, 256))
493
resized.save(f"exports/{texture.name}_resized.png")
494
```
495
496
### Processing Audio Clips
497
498
```python
499
import UnityPy
500
501
env = UnityPy.load("audio_assets/")
502
503
for obj in env.objects:
504
if obj.type.name == "AudioClip":
505
audio = obj.read()
506
507
print(f"Audio: {audio.name}")
508
print(f"Length: {audio.m_Length:.2f}s")
509
print(f"Sample Rate: {audio.m_Frequency}Hz")
510
print(f"Channels: {audio.m_Channels}")
511
512
# Export as WAV
513
audio.save(f"exports/{audio.name}.wav")
514
515
# Get raw samples for processing
516
samples = audio.samples
517
print(f"Sample data shape: {samples.shape}")
518
```
519
520
### Extracting Mesh Data
521
522
```python
523
import UnityPy
524
525
env = UnityPy.load("mesh_assets/")
526
527
for obj in env.objects:
528
if obj.type.name == "Mesh":
529
mesh = obj.read()
530
531
print(f"Mesh: {mesh.name}")
532
print(f"Vertices: {len(mesh.vertices)}")
533
print(f"Triangles: {len(mesh.triangles) // 3}")
534
535
# Access vertex data
536
for i, vertex in enumerate(mesh.vertices[:5]): # First 5 vertices
537
print(f"Vertex {i}: ({vertex.x}, {vertex.y}, {vertex.z})")
538
539
# Access UV coordinates if available
540
if mesh.uv:
541
print(f"UV coordinates: {len(mesh.uv)}")
542
```
543
544
### Modifying Game Objects
545
546
```python
547
import UnityPy
548
549
env = UnityPy.load("scene_assets/")
550
551
for obj in env.objects:
552
if obj.type.name == "GameObject":
553
gameobj = obj.read()
554
555
# Modify properties
556
gameobj.name = f"Modified_{gameobj.name}"
557
gameobj.m_IsActive = False
558
559
# Save changes
560
obj.save(gameobj)
561
562
# Save modified assets
563
env.save(out_path="modified_scene/")
564
```
565
566
### Using Type Tree for Unknown Objects
567
568
```python
569
import UnityPy
570
571
env = UnityPy.load("custom_assets/")
572
573
for obj in env.objects:
574
# For any object type, even unknown ones
575
raw_data = obj.read_typetree()
576
577
print(f"Object type: {obj.type.name}")
578
print(f"Properties: {list(raw_data.keys())}")
579
580
# Modify via dictionary
581
if "m_Name" in raw_data:
582
raw_data["m_Name"] = f"Modified_{raw_data['m_Name']}"
583
obj.save_typetree(raw_data)
584
```