A pythonic port of AssetStudio by Perfare
Project description
UnityPy
A Unity asset extractor for Python based on AssetStudio.
Next to extraction it also supports editing Unity assets. So far following obj types can be edited:
- Texture2D
- Sprite(indirectly via linked Texture2D)
- TextAsset
- MonoBehaviour (and all other types that you have the typetree of)
If you need advice or if you want to talk about (game) data-mining, feel free to join the UnityPy Discord.
If you're making an income by using UnityPy, please donate a small part of it to a charitable cause of your choice or sponsor this project with a small amount.
Installation
Python 3.6.0 or higher is required
pip install UnityPy
or download/clone the git and use
python setup.py install
Notes
Windows
Visual C++ Redistributable is required for the brotli dependency.
Example
The following is a simple example.
import os
import UnityPy
def unpack_all_assets(source_folder : str, destination_folder : str):
# iterate over all files in source folder
for root, dirs, files in os.walk(source_folder):
for file_name in files:
# generate file_path
file_path = os.path.join(root, file_name)
# load that file via UnityPy.load
env = UnityPy.load(file_path)
# iterate over internal objects
for obj in env.objects:
# process specific object types
if obj.type.name in ["Texture2D", "Sprite"]:
# parse the object data
data = obj.read()
# create destination path
dest = os.path.join(destination_folder, data.name)
# make sure that the extension is correct
# you probably only want to do so with images/textures
dest, ext = os.path.splitext(dest)
dest = dest + ".png"
img = data.image
img.save(dest)
# alternative way which keeps the original path
for path,obj in env.container.items():
if obj.type.name in ["Texture2D", "Sprite"]:
data = obj.read()
# create dest based on original path
dest = os.path.join(destination_folder, *path.split("/"))
# make sure that the dir of that path exists
os.makedirs(os.path.dirname(dest), exist_ok = True)
# correct extension
dest, ext = os.path.splitext(dest)
dest = dest + ".png"
data.image.save(dest)
You probably have to read Important Classes and Important Object Types to understand how it works.
People who have slightly advanced python skills should take a look at UnityPy/tools/extractor.py for a more advanced example. It can also be used as the general template or simply as an importable tool.
Important Classes
Environment
Environment loads and parses the files that are given to it. It can be initialized via:
- a file path - apk files can be loaded as well
- a folder path - loads all files in that folder (bad idea for folders with a lot of files)
- a stream - e.g. io.BytesIO, file stream,...
- a bytes object - will be loaded into a stream
UnityPy can detect itself if the file is a WebFile, BundleFile, Asset, or APK itself.
The unpacked assets will be loaded into .files
, which is a dict consisting of asset-name : asset
.
All objects of the loaded assets can be easily accessed via .objects
,
which itself is a simple recursive iterator.
import io
import UnityPy
# all of the following would work
src = "file_path"
src = b"bytes"
src = io.BytesIO(b"Streamable")
env = UnityPy.load(src)
for obj in env.objects:
...
# saving an edited file
# apply modifications to the objects
# don't forget to use data.save()
...
with open(dst, "wb") as f:
f.write(env.file.save())
Asset
Assets are a container that contains multiple objects. One of these objects can be an AssetBundle, which contains a file path for some of the objects in the same asset.
All objects can be found in the .objects
dict - {ID : object}
.
The objects which have a file path can be found in the .container
dict - {path : object}
.
Object
Objects contain the actual files which, e.g. textures, text files, meshes, settings, ...
To acquire the actual data of an object it has to be read first, this happens via the .read()
function. This isn't done automatically to save time because only a small part of the objects are of interest. Serialized objects can be set with raw data using .set_raw_data(data)
or modified with .save()
function if supported.
Important Object Types
All object types can be found in UnityPy/classes.
Texture2D
.name
.image
converts the texture into aPIL.Image
.m_Width
- texture width (int).m_Height
- texture height (int)
Export
from PIL import Image
for obj in env.objects:
if obj.type.name == "Texture2D":
# export texture
data = image.read()
data.image.save(path)
# edit texture
fp = os.path.join(replace_dir, data.name)
pil_img = Image.open(fp)
data.image = pil_img
data.save()
Sprite
Sprites are part of a texture and can have a separate alpha-image as well. Unlike most other extractors (including AssetStudio) UnityPy merges those two images by itself.
.name
.image
- converts the merged texture part into aPIL.Image
.m_Width
- sprite width (int).m_Height
- sprite height (int)
Export
for obj in env.objects:
if obj.type.name == "Sprite":
data = image.read()
data.image.save(path)
TextAsset
TextAssets are usually normal text files.
.name
.script
- binary data (bytes).text
- script decoded via UTF8 (str)
Some games save binary data as TextFile, so it's usually better to use .script
.
Export
for obj in env.objects:
if obj.type.name == "TextAsset":
# export asset
data = image.read()
with open(path, "wb") as f:
f.write(bytes(data.script))
# edit asset
fp = os.path.join(replace_dir, data.name)
with open(fp, "rb") as f:
data.script = f.read()
data.save()
MonoBehaviour
MonoBehaviour assets are usually used to save the class instances with their values. If a type tree exists it can be used to read the whole data, but if it doesn't, then it is usually necessary to investigate the class that loads the specific MonoBehaviour to extract the data. (example)
.name
.script
.raw_data
- data after the basic initialisation
Export
import json
for obj in env.objects:
if obj.type.name == "MonoBehaviour":
# export
if obj.serialized_type.nodes:
# save decoded data
tree = obj.read_typetree()
fp = os.path.join(extract_dir, f"{data.name}.json")
with open(fp, "wt", encoding = "utf8") as f:
json.dump(tree, f, ensure_ascii = False, indent = 4)
else:
# save raw relevant data (without Unity MonoBehaviour header)
data = obj.read()
fp = os.path.join(extract_dir, f"{data.name}.bin")
with open(fp, "wb") as f:
f.write(data.raw_data)
# edit
if obj.serialized_type.nodes:
tree = obj.read_typetree()
# apply modifications to the data within the tree
obj.save_typetree(tree)
else:
with open(replace_dir, data.name) as f:
data.save(raw_data = f.read())
AudioClip
.samples
-{sample-name : sample-data}
The samples are converted into the .wav format. The sample data is a .wav file in bytes.
clip : AudioClip
for name, data in clip.samples.items():
with open(name, "wb") as f:
f.write(data)
Font
if obj.type.name == "Font":
font : Font = obj.read()
if font.m_FontData:
extension = ".ttf"
if font.m_FontData[0:4] == b"OTTO":
extension = ".otf"
with open(os.path.join(path, font.name+extension), "wb") as f:
f.write(font.m_FontData)
Mesh
.export()
- mesh exported as .obj (str)
The mesh is converted into a Wavefront .obj file.
mesh : Mesh
with open(f"{mesh.name}.obj", "wt", newline = "") as f:
# newline = "" is important
f.write(mesh.export())
Renderer, MeshRenderer, SkinnedMeshRenderer
ALPHA-VERSION
.export(export_dir)
- exports the associated mesh, materials, and textures into the given directory
The mesh and materials will be in the Wavefront formats.
mesh_renderer : Renderer
export_dir: str
if mesh_renderer.m_GameObject:
# get the name of the model
game_object = mesh_renderer.m_GameObject.read()
export_dir = os.path.join(export_dir, game_object.name)
mesh_renderer.export(export_dir)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for UnityPy-1.8.11-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96596cae543385abe9db46c649cdd3c02ec9aa891f5a74db9f9acc14ecb22c76 |
|
MD5 | 8d03e76058b3775cdd8d3b91d23cfc12 |
|
BLAKE2b-256 | b42b5ecb51db0e34a5b177e3804eda07d94fa9440128ebe609e5537962f70b67 |
Hashes for UnityPy-1.8.11-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 766212d0c7353349fc136e74391f5bc557ad0451b057bbe24fecbe45056e5d1b |
|
MD5 | 2f7b5726c9b55a233ad42ebac54d9161 |
|
BLAKE2b-256 | a7ee36638d69ec812a1834ebd7945a0f915dd898d68f537469f8b15fc6a95e89 |
Hashes for UnityPy-1.8.11-cp310-cp310-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 178ad477732e4f6bcebabb4e0ddfadaf605d2e7e0e0466183af8d0a3e6eedb98 |
|
MD5 | e45bb863976296ca8727df57e291fd63 |
|
BLAKE2b-256 | 27fccfa8da506c6c96653c0c06386b80b392f37535e45595a92729693b978cbb |
Hashes for UnityPy-1.8.11-cp310-cp310-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c10c09fec2c1562023f6ffaf72a92d3aab9cc3b78b3a2c7b86cf1bacfee45b61 |
|
MD5 | 718e086105ab837d9ce146120b1a85f7 |
|
BLAKE2b-256 | add3915d8ad62c7a93cbe449415645f3d4253328d860e04fada3f7a5638e0824 |
Hashes for UnityPy-1.8.11-cp310-cp310-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe650b6424f8a8cfdbeda32834c38320fec53f987f9f475f41f53d33241b5d02 |
|
MD5 | 31dc2e180d14e7675269eff9aa05a612 |
|
BLAKE2b-256 | b7a42bc4010cf6223b0427ae8a67b87a137162ad32ec821e6bfc6d7d2f3ba859 |
Hashes for UnityPy-1.8.11-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad5104326bb8267914e79248f91b8871b3b71b2822c3f4c691c31f3228f108ce |
|
MD5 | 1d71ec26da4889d6e1b34fe61ed941f3 |
|
BLAKE2b-256 | 7c7cfd754b5e05c73be195ab2d6cd54493289f810ca933dd0f9686cbf4463798 |
Hashes for UnityPy-1.8.11-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14a0342e3c00683c3fab370b592c9e77f639b64b39b0331ba67ac2a119884044 |
|
MD5 | bc2f22ab3681ead8cad776c535fa015d |
|
BLAKE2b-256 | b254865bdf3b57a4906aa2251fcb34cf23054a33898f7a6096e690b5a740ae5c |
Hashes for UnityPy-1.8.11-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16dbf412e19c1d414b13a3f7c043e5ffe210291849e6b941dba54f4591b6127a |
|
MD5 | c12062dc754e60fceb929cf751a13f24 |
|
BLAKE2b-256 | 3f8f1fda49fca058c95d53f580775fb7fbf5edc5a2749141799fde8811677b4c |
Hashes for UnityPy-1.8.11-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b14c4098baed4e3c30702aa0c687e31b254a74879956719bb10353ce0f23ddd |
|
MD5 | 3a589719baeabf17350bf86ac4fdbb9d |
|
BLAKE2b-256 | 02c58991196683e4994970cfd1f358a09650afc41a06085277a1a069e3e1999a |
Hashes for UnityPy-1.8.11-cp39-cp39-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1804d4607295afe19421c52ad0b645223dbd8e772acbea3c5e86f1dd7ed4988d |
|
MD5 | ea0b09bbd21f9d57d37e39ea3c9d6eee |
|
BLAKE2b-256 | 8c409614b9c6763cce3edb621fa5424d7040581d968e2a168400a0c776f95550 |
Hashes for UnityPy-1.8.11-cp39-cp39-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7824d9887fca29a8758aff99b17c03924538673fc2174bc989af41ccd5cc47d1 |
|
MD5 | 66c5b0361a75abdac5895c5c87ae1d35 |
|
BLAKE2b-256 | fa8d8690c9fc3d6e0fed194883163635e60ec61120482358eb51ba18b34e8351 |
Hashes for UnityPy-1.8.11-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ca63e38ab4c9c86f302a30877313709b27defb3a3bf6a5dcc9a4e5fed5f3138 |
|
MD5 | 903d04970634f402ca7fae67ea97b4b0 |
|
BLAKE2b-256 | c21b46ca3d02d5aeaa1b69ac053caf3bb8ed101c17cb8c9949d80342cfe32615 |
Hashes for UnityPy-1.8.11-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72ec5ac063c4a0b9bacd4d88bbbd21e9431e8d7477ac13cb945aceb6f40a0a4e |
|
MD5 | 0c912a88b9cd34b48d1ba2d196351c12 |
|
BLAKE2b-256 | 661e6135204910f75716a5937e6cf93a6417017a519d1520a3ffdc55116a8bd2 |
Hashes for UnityPy-1.8.11-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 481e47dcef89022900544fa4775cc5ebfb52173094528a9b6708f3c840fcbbf6 |
|
MD5 | 36e45abd34a0971e8814d6ee8f8223d7 |
|
BLAKE2b-256 | 02e7c473b35f79fa9f0029d6c4c56c0f3782b60de069629b6e763eece5b76acf |
Hashes for UnityPy-1.8.11-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb51f0e36ebb367bae783896a201e663cfa195957a24a65f3c82b7c123835c5c |
|
MD5 | 61f1aed7b270a56fb97f932c23af3f33 |
|
BLAKE2b-256 | 4f9a8625cc66c53e02a5be0d3fb4b06f3669d3e3a33c9dd62e7787817a9e5c00 |
Hashes for UnityPy-1.8.11-cp38-cp38-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb9140366964ce39b620bcd52263143e12f49e7e82907272798bf2a0685de673 |
|
MD5 | e2930440be96c90a2b92c642a27cd837 |
|
BLAKE2b-256 | 9feae0e95592c115f7732102f2faaa479c8beb9ab7f36897b2ad2724270869fb |
Hashes for UnityPy-1.8.11-cp38-cp38-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20c3a2859fcc8e043e7155d08a3ce0ece718b518ad666185e0a928f8da6e78bc |
|
MD5 | 29989d588a4068c3077f9de13967fa89 |
|
BLAKE2b-256 | 7e15bf4586d2913a6bd16834d82ba65af58d5845fdd8ce97c6e5fe16bdc14497 |
Hashes for UnityPy-1.8.11-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 330da69a17c3a1847fa29a19382f7a3e02874145d611407e6d209ec46b6eea06 |
|
MD5 | 7eed001a9535c0b71fc2f56c4a8f3637 |
|
BLAKE2b-256 | 564216ab3d17ec7cbb8d5233fdf9e82f9c1bd357be79a8dabc086ffc1e33a568 |
Hashes for UnityPy-1.8.11-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ee58e77749b00664796f0ed3fddf3c3209bed7c8e1c72605c370f1d04d38485 |
|
MD5 | 4f2400f700d8e95ee93c605b5862c99d |
|
BLAKE2b-256 | 6f7c5012cb61666e34f9743db70455d4ecbe3ac672da3bf04a9e63595d5b7789 |
Hashes for UnityPy-1.8.11-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f13fd808385a4f1b3ea4d58d9db487974a09cfdb4f4819e482ee99f490f84e1 |
|
MD5 | c3b6be501486052e5a732557558c779f |
|
BLAKE2b-256 | 9f032a81ad5fd41427f8211ea23b2d8c7e74c3c17afc3fe7df6504d48c42f34b |
Hashes for UnityPy-1.8.11-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c98d1e241d7934e066668b54a70cf7172f78300db5fa7d07caf6e989b0f462a7 |
|
MD5 | b8f923897422f58d6381793addb1aece |
|
BLAKE2b-256 | 651611144903176c41ca068485ad0656b56403059e44d1dce48ab7e3fc6ea2ef |
Hashes for UnityPy-1.8.11-cp37-cp37m-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d508f23b9e210e872dabc685e33470f73783fdb5b0ebd8946c7d6d5054159aa1 |
|
MD5 | ff1727e42789ed3da5c27411d42fdfcf |
|
BLAKE2b-256 | c8a2c493c3b02e7a8b88df6359a11a8e70ea25c7337ca11928d57d7e4b4f1b66 |
Hashes for UnityPy-1.8.11-cp37-cp37m-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24ae8c05c55745a30e3ee5096c933e0774ef8aa07ded88db55cc6853947bfd6e |
|
MD5 | 27d48a1e21b8bb373bd2333b9662183c |
|
BLAKE2b-256 | b2c2eabbea0a709976754faff21c7dec562c42a3533265268c245cee95cc1915 |
Hashes for UnityPy-1.8.11-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7176d30e947e29e728080865e1c50e434d9502298fcaf1e43b9c9590fa5347e0 |
|
MD5 | 8480843f9a6ac832fb9c11038a19c9db |
|
BLAKE2b-256 | 078d2ecce7497119dc7efb36e69bf6d8c9180f1db36faaf3ea9a0425f337b4c1 |