How to Reverse Engineer a Unity Game
In this tutorial, you’ll use ILSpy and AssetStudio to extract code and assets from a compiled Unity game. By Eric Van de Kerckhove.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Reverse Engineer a Unity Game
15 mins
Exporting Code
You can save the code by copy-pasting it into a file, or by right-clicking the class in the Assemblies list and selecting Save Code… in the menu. You can now choose a location to save the C# file to. Don’t worry, you’re free to copy my amazing code and use it for your own. :]
In large assemblies, having to inspect and save the classes separately can get tedious, so I’ll introduce you to a more efficient way of saving the code. Right-click on Assembly-CSharp and choose Save Code… in the menu. This will prompt you to choose a location to save the C# project file to. It might seem like you’re saving a single file here, but ILSpy will actually copy over all classes it can find as C# files, along with a project file.
Generally, ILSpy does a pretty good job of decompiling the code, but you may run into some weird code here and there if the decompiler wasn’t sure what to do. For instance, if you take a look at the BrokenResponsibility class, it has a DestroySelf
method which differs from the original code. The decompiled version looks like this:
private void DestroySelf()
{
Object.Destroy(base.gameObject);
}
While the original code was the following:
private void DestroySelf()
{
Destroy(gameObject);
}
Note the Object
class and base
keyword that were added to the method call. The decompiled code is still valid, but you might want to consider cleaning it up a bit when using it in your own projects.
That concludes the use of ILSpy! It’s a powerful tool to keep in mind when you need to extract the source code from Unity games and other .NET based products. It has saved me a few times already when I lost my projects due to data corruption. Backups are better in every way, but this is a nice backup. Just remember to be nice and use it for good only.
Now you know how to inspect and save source code, it’s time to move on to extracting the assets.
Extracting Assets
AssetStudio is an open-source tool that can inspect and extract the following asset types from Unity 3.4 to 2022.1:
- Textures and sprites
- Audio
- Fonts
- Meshes
- Text
- Shaders
- Video files
- Animations
It does this by reverse engineering the packed formats and converting those back into usable formats. A texture for example can be converted into png, tga, jpeg or bmp.
Open AssetStudio by opening the folder you extracted in the Getting Started section and double-click AssetStudioGUI.exe. After a short while, you’ll be greeted by its user interface:
Tour of AssetStudio
Like ILSpy, AssetStudio has two main sections: a file browser on the left and a preview on the right. Unlike ILSpy however, there’s nothing to see here until you open an assets file or a folder of a compiled game.
Before delving deeper in the application, disable all error messages as at the time of writing AssetStudio has some issues with loading in shaders, resulting in a ton of errors being generated. To ignore these, select Debug in the toolbar and uncheck Show error message.
Now load in the assets by selecting File ▸ Load folder in the toolbar to open a folder browser window.
Navigate to the Avoiding Responsibility folder, you know you’re at the right place if you see the Avoiding Responsibility_Data folder:
Select this folder to load it. AssetStudio will now search all subfolders for any asset files and read their contents. This can take quite a while depending on the amount of assets and their type. As long as the progress bar at the bottom is moving and the text field below the preview reads “Read assets…” you should be good. It took my system about two minutes before the application was finally done loading.
Once the assets are loaded, you should see a list of scenes and prefabs appearing in the Scene Hierarchy tab:
You can expand these to get a rough idea of what sort of GameObjects make up the scene, but apart from that it’s not that useful.
Exporting Assets
Open the Asset List tab, as that’s where the fun begins. In here, you can find all assets supported by AssetStudio that are part of the sample game. Most of these are included by Unity, but some were added by me when creating the game.
The list can be filtered by name if you know what to look for by using the textbox below the tabs. For example, type in “Entertainer” and wait a short while, the list will shrink down to a single AudioClip of the background music.
Preview the file by clicking on its name in the Asset List and pressing the Play button in the Preview window on the right.
Click on the Stop button once you’re done enjoying the tune. You can save this asset by right-clicking its name and selecting Export selected assets in the menu. This will prompt you to choose a folder to save it to.
AssetStudio will create a folder based on the type of asset, AudioClip in this case. Inside you’ll find the audio file in WAV format.
Clear out the filter by selecting the text you’ve added and hitting Backspace or Delete on your keyboard. There’s another way to filter assets: by their type.
Select Filter Type ▸ Mesh in the toolbar to only show 3D models in the list. The models used in the sample game all have the same name: default. You can select them one by one to get a nice preview.
Now select all default meshes, right-click on any of them and select Export selected Assets in the menu. Select a folder to save the models to in the folder browser window like before.
Once the models are exported, they’re all in OBJ format. You can use an application like Blender to import these files, edit them and export to a format of your choosing. You can also use them as-is, as most game engines support OBJ meshes.
Here’s the default.obj file opened up in Blender as an example:
That’s it for AssetStudio, another fine tool to have in your arsenal.