forked from 641i130/klbvfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract.py
66 lines (56 loc) · 2.75 KB
/
extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import UnityPy
from PIL import Image
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)
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()
if obj.type.name == "Mesh":
mesh : Mesh = obj.read()
with open(f"{mesh.name}.obj", "wt", newline = "") as f:
# newline = "" is important
f.write(mesh.export())
#copypaste of the renderer export method here: https://pypi.org/project/UnityPy/#important-object-types
if obj.type.name == "Renderer":
mesh_renderer : Renderer = obj.read()
if mesh_renderer.m_GameObject:
game_object = mesh_renderer.m_GameObject.read()
export_dir = os.path.join(destination_folder, game_object.name)
mesh_renderer.export(export_dir)
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)
#unpack_all_assets(".",".")