DEV Community


Posted on

Extracting content from Unity packages in terminal

While I was researching some physics mathsy stuff, the only actual source code implementation I could find was buried inside a Unity asset thing. I don't use Unity, but as far as I know you can only download Unity packages through the Unity editor, not through the Unity asset store website, and it still won't seem to let you browse through the asset's source.

Regardless, here's a method to extract Unity unitypackage files properly and not just the GUID names with hexadecimal directories.

For example the Standard Assets package:

$ file Standard\ Assets.unitypackage
Standard Assets.unitypackage: gzip compressed data, extra field,
last modified: Mon Mar  5 16:28:50 2018, max compression,
from Unix, original size 250911232
Enter fullscreen mode Exit fullscreen mode

Not only is the file gzipped, but underneath it's also a tar archive, so we can extract everything like so:

$ tar zxfv Standard\ Assets.unitypackage --one-top-level=Standard\ Assets
Enter fullscreen mode Exit fullscreen mode

All the package content has now been extracted into the Standard Assets directory, but it's not very usable as there are tons of directories with meaningless hexchar GUID names; but they all contain a pathname file that we can process and use to organise the data with this simple oneliner:

$ find -name asset -exec bash -c '\
    filename="`dirname "$(dirname "{}")"`/`cat "$(dirname "{}")/pathname" \
        | cut -d$'"'\n'"' -f1 \
        | tr -d "\n"`"; \
    filedir=`dirname "$filename"`; \
    mkdir -p "$filedir"; \
    echo "Extracting \"$filename\"..."; \
    cp "{}" "$filename"' \;
Enter fullscreen mode Exit fullscreen mode

Having done that the intermediate directories can be cleaned up:

$ find -type d -regextype sed -regex '.*[0-9a-f]\{32\}' | xargs -I{} rm -rf "{}"
Enter fullscreen mode Exit fullscreen mode

All the files should now be extracted properly and you can poke around with them! Hooray!

$ ls Standard\ Assets/Assets/Standard\ Assets/
2D  Cameras  Characters  CrossPlatformInput  Editor  Effects  Environment
Fonts  ParticleSystems  PhysicsMaterials  Prototyping  Utility  Vehicles
Enter fullscreen mode Exit fullscreen mode

You can untar multiple unitypackages before running the oneliner and it will extract all of them at once!

Top comments (1)

tacman profile image
Tac Tacelosky

Very slick, thanks! An elegant solution.