Skip to content

Commit bc1929c

Browse files
committed
Update README.md
1 parent 52dad38 commit bc1929c

1 file changed

Lines changed: 80 additions & 37 deletions

File tree

‎README.md‎

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,47 @@ The following is a simple example.
3535

3636
```python
3737
import os
38-
from UnityPy import AssetsManager
38+
import UnityPy
3939

4040
def unpack_all_assets(source_folder : str, destination_folder : str):
4141
# iterate over all files in source folder
4242
for root, dirs, files in os.walk(source_folder):
4343
for file_name in files:
4444
# generate file_path
4545
file_path = os.path.join(root, file_name)
46-
# load that file via AssetsManager
47-
am = AssetsManager(file_path)
48-
49-
# iterate over all assets and named objects
50-
for asset in am.assets.values():
51-
for obj in asset.objects.values():
52-
# only process specific object types
53-
if obj.type in ["Texture2D", "Sprite"]:
54-
# parse the object data
55-
data = obj.read()
56-
57-
# create destination path
58-
dest = os.path.join(destination_folder, data.name)
59-
60-
# make sure that the extension is correct
61-
# you probably only want to do so with images/textures
62-
dest, ext = os.path.splitext(dest)
63-
dest = dest + ".png"
64-
65-
img = data.image
66-
img.save(dest)
46+
# load that file via UnityPy.load
47+
env = UnityPy.load(file_path)
48+
49+
# iterate over internal objects
50+
for obj in env.objects:
51+
# process specific object types
52+
if obj.type in ["Texture2D", "Sprite"]:
53+
# parse the object data
54+
data = obj.read()
55+
56+
# create destination path
57+
dest = os.path.join(destination_folder, data.name)
58+
59+
# make sure that the extension is correct
60+
# you probably only want to do so with images/textures
61+
dest, ext = os.path.splitext(dest)
62+
dest = dest + ".png"
63+
64+
img = data.image
65+
img.save(dest)
66+
67+
# alternative way which keeps the original path
68+
for path,obj in env.container.items():
69+
if obj.type in ["Texture2D", "Sprite"]:
70+
data = obj.read()
71+
# create dest based on original path
72+
dest = os.path.join(destination_folder, *path.split("/"))
73+
# make sure that the dir of that path exists
74+
os.makedirs(os.path.dirname(dest), exist_ok = True)
75+
# correct extension
76+
dest, ext = os.path.splitext(dest)
77+
dest = dest + ".png"
78+
data.image.save(dest)
6779
```
6880

6981
You probably have to read [Important Classes](#important-classes)
@@ -74,9 +86,9 @@ People who have slightly advanced python skills should take a look at [AssetBatc
7486

7587
## Important Classes
7688

77-
### [AssetsManager](UnityPy/AssetsManager.py)
89+
### [Enivironment](UnityPy/environment.py)
7890

79-
AssetsManager loads and parses the files that are given to it.
91+
Environment loads and parses the files that are given to it.
8092
It can be initialized via:
8193

8294
* a file path - apk files can be loaded as well
@@ -86,11 +98,11 @@ It can be initialized via:
8698

8799
UnityPy can detect itself if the file is a WebFile, BundleFile, Asset or APK itself.
88100

89-
The unpacked assets will be loaded into ``.assets``, which is a dict consisting of ``asset-name : asset``.
101+
The unpacked assets will be loaded into ``.files``, which is a dict consisting of ``asset-name : asset``.
90102

91103
```python
92-
from UnityPy import AssetsManager
93-
am = AssetsManager(src)
104+
import UnityPy
105+
env = UnityPy.load(src)
94106

95107
for asset_name, asset in am.assets.items():
96108
pass
@@ -122,6 +134,14 @@ All object types can be found in [UnityPy/classes](UnityPy/classes/).
122134
* ``.m_Width`` - texture width (int)
123135
* ``.m_Height`` - texture height (int)
124136

137+
__Export__
138+
```python
139+
for obj in env.objects:
140+
if obj.type == "Texture2D":
141+
data = image.read()
142+
data.image.save(path)
143+
```
144+
125145
### [Sprite](UnityPy/classes/Sprite.py)
126146

127147
Sprites are part of a texture and can have a separate alpha-image as well.
@@ -132,6 +152,14 @@ Unlike most other extractors (including AssetStudio) UnityPy merges those two im
132152
* ``.m_Width`` - sprite width (int)
133153
* ``.m_Height`` - sprite height (int)
134154

155+
__Export__
156+
```python
157+
for obj in env.objects:
158+
if obj.type == "Sprite":
159+
data = image.read()
160+
data.image.save(path)
161+
```
162+
135163
### [TextAsset](UnityPy/classes/TextAsset.py)
136164

137165
TextAssets are usually normal text files.
@@ -142,14 +170,37 @@ TextAssets are usually normal text files.
142170

143171
Some games save binary data as TextFile, so it's usually better to use ``.script``.
144172

173+
__Export__
174+
```python
175+
for obj in env.objects:
176+
if obj.type == "TextAsset":
177+
data = image.read()
178+
with open(path, "wb") as f:
179+
f.write(bytes(data.script))
180+
```
181+
145182
### [MonoBehaviour](UnityPy/classes/MonoBehaviour.py)
146183

147184
MonoBehaviour assets are usually used to save the class instances with their values.
148185
If a type tree exists it can be used to read the whole data,
149186
but if it doesn't, then it is usually necessary to investigate the class that loads the specific MonoBehaviour to extract the data.
187+
([example](examples/CustomMonoBehaviour/get_scriptable_texture.py))
150188

151189
* ``.name``
152-
* ``.script``- binary data (bytes)
190+
* ``.script``
191+
* ``.raw_data`` - data after the basic initialisation
192+
193+
__Export__
194+
```python
195+
import json
196+
197+
for obj in env.objects:
198+
if obj.type == "MonoBehaviour":
199+
data = image.read()
200+
if data.type_tree:
201+
with open(path, "wt", encoding="utf8") as f:
202+
json.dump(data.type_tree.to_dict(), f, ensure_ascii = False, indent=4)
203+
```
153204

154205
### [AudioClip](UnityPy/classes/AudioClip.py)
155206

@@ -169,15 +220,7 @@ for name, data in clip.samples.items():
169220

170221
### WIP
171222

172-
- [x] A documentation
173-
- [x] The ability to edit assets (like in UABE)
174-
175-
### TODO
176-
177-
- [ ] Support for more object types
178-
- [ ] Code optimization
179-
- [ ] Speed-ups via C-extensions
180-
- [ ] Multiprocessing
223+
- [] documentation
181224

182225
## Motivation
183226

0 commit comments

Comments
 (0)