UnityPy is a powerful Python-based tool that simplifies the extraction and editing of Unity assets. Built on the foundation of AssetStudio, UnityPy allows you to manipulate various asset types within Unity, such as textures, sprites, and text files. In this article, we will guide you through the installation, basic usage, and some troubleshooting tips to make your experience smooth and effective.
Installation
To get started with UnityPy, ensure that you have Python 3.7.0 or higher installed on your machine. There are two ways to install UnityPy:
- Via pip:
cmdpip install UnityPy
- From source:
cmdgit clone https://github.com/K0lb3/UnityPy.git
and thencd UnityPy
followed bypython -m pip install .
Understanding the Code: Analogy
Let’s take a simple analogy to understand how the example code works in UnityPy. Imagine you’re a librarian who has a collection of books (assets) stored in various boxes (folders). You want to extract all the books and check their content (read asset data).
The code can be visualized in the following steps:
- You walk through each box (source_folder), just as the code iterates through each file in the folder.
- When you find a book (a file), you open it (load the file using UnityPy) to check what’s inside.
- If the book contains a picture (specific object types like Texture2D or Sprite), you make a copy (save the image as a PNG) to keep in your collection (destination_folder).
This analogy illustrates how the code works step by step to help you extract valuable content from Unity assets effectively.
Basic Example
Here’s a simple example of how to extract assets using UnityPy:
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 + ".png")
img = data.image
img.save(dest)
This function walks through a specified folder and extracts textures and sprites by saving them in the defined destination folder.
Troubleshooting Tips
As you dive into using UnityPy, you may encounter some common issues. Here are a few troubleshooting tips:
- If you experience a crash without warning, try disabling the C-typetree reader by adding these lines to your main file:
from UnityPy.helpers import TypeTreeHelper TypeTreeHelper.read_typetree_c = False
- If UnityPy doesn’t function correctly with the latest version of Python, consider downgrading to the last supported version.
- In case you face issues compiling C-dependencies with new Python versions, refer to the solution provided in this GitHub issue.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Wrap Up
UnityPy offers a comprehensive solution for extracting and editing Unity assets seamlessly. By following the steps outlined in this blog, you will be well on your way to handling various asset types with ease.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.