UnityPack icon indicating copy to clipboard operation
UnityPack copied to clipboard

Support for unpacking sprites

Open andyearnshaw opened this issue 7 years ago • 5 comments

A lot of the bundles I'm extracting have sprite atlases in them and it's pretty tedious to manually cut them out and rotate them to the correct position. I've been looking for an automated way to do this with no luck.

https://docs.unity3d.com/Manual/SpritePacker.html

How difficult would this be?

andyearnshaw avatar Oct 06 '17 12:10 andyearnshaw

If you can find the spatial and rotational metadata for each sprite then it could be added as a utility method, iff it's consistent.

jleclanche avatar Oct 06 '17 13:10 jleclanche

I think it would be consistent since (according to the link above) it's handled by Unity automatically. If I export the YAML for one such bundle, there are some dictionaries that contain spriteDefinition metadata:

!!python/object/apply:collections.OrderedDict
- - - m_GameObject
    - !PPtr [0, -1300960676681644065]
  - [m_Enabled, 1]
  - - m_Script
    - !PPtr [0, 5287704178048467122]
  - [m_Name, '']
  - [version, 3]
  - [materialIdsValid, 1]
  - [needMaterialInstance, 0]
  - - spriteDefinitions
    - - !!python/object/apply:collections.OrderedDict
        - - [name, Fry]
          - - boundsData
            - - !!python/object/apply:collections.OrderedDict
                - - [x, 0.004999995231628418]
                  - [y, -0.004999995231628418]
                  - [z, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 2.309999942779541]
                  - [y, 3.309999942779541]
                  - [z, 0.0]
          - - untrimmedBoundsData
            - - !!python/object/apply:collections.OrderedDict
                - - [x, 0.004999995231628418]
                  - [y, -0.004999995231628418]
                  - [z, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 2.309999942779541]
                  - [y, 3.309999942779541]
                  - [z, 0.0]
          - - texelSize
            - !!python/object/apply:collections.OrderedDict
              - - [x, 0.009999999776482582]
                - [y, 0.009999999776482582]
          - - positions
            - - !!python/object/apply:collections.OrderedDict
                - - [x, -1.149999976158142]
                  - [y, -1.659999966621399]
                  - [z, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 1.159999966621399]
                  - [y, -1.659999966621399]
                  - [z, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, -1.149999976158142]
                  - [y, 1.649999976158142]
                  - [z, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 1.159999966621399]
                  - [y, 1.649999976158142]
                  - [z, 0.0]
          - - normals
            - []
          - - tangents
            - []
          - - uvs
            - - !!python/object/apply:collections.OrderedDict
                - - [x, 0.0009770507458597422]
                  - [y, 0.0009770507458597422]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 0.1137690395116806]
                  - [y, 0.0009770507458597422]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 0.0009770507458597422]
                  - [y, 0.1625971645116806]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 0.1137690395116806]
                  - [y, 0.1625971645116806]
          - - normalizedUvs
            - - !!python/object/apply:collections.OrderedDict
                - - [x, 0.0]
                  - [y, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 1.0]
                  - [y, 0.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 0.0]
                  - [y, 1.0]
              - !!python/object/apply:collections.OrderedDict
                - - [x, 1.0]
                  - [y, 1.0]
          - - indices
            - [0, 3, 1, 2, 3, 0]
          - - material
            - !PPtr [0, -139266242881986336]
          - [materialId, 0]
          - [sourceTextureGUID, f7e9e7708ec334bd596cfa71cd2c503d]
          - [extractRegion, 0]
          - [regionX, 0]
          - [regionY, 0]
          - [regionW, 0]
          - [regionH, 0]
          - [flipped, 0]
          - [complexGeometry, 0]
          - [physicsEngine, 0]
          - [colliderType, 0]
          - - customColliders
            - []
          - - colliderVertices
            - []
          - - colliderIndicesFwd
            - []
          - - colliderIndicesBack
            - []
          - [colliderConvex, 0]
          - [colliderSmoothSphereCollisions, 0]
          - - polygonCollider2D
            - []
          - - edgeCollider2D
            - []
          - - attachPoints
            - []
    # more spriteDefinition objects
    # ....
  - [premultipliedAlpha, 0]
  - [material, null]
  - - materials
    - - !PPtr [0, -139266242881986336]
  - - textures
    - - !PPtr [0, 8453760564669429443]
  - - pngTextures
    - []
  - - materialPngTextureId
    - [0]
  - [textureFilterMode, 1]
  - [textureMipMaps, 0]
  - [allowMultipleAtlases, 0]
  - [spriteCollectionGUID, b1383bcabe03a4c72a84202e345167e7]
  - [spriteCollectionName, '!Cards']
  - [assetName, Cards]
  - [loadable, 1]
  - [invOrthoSize, 2.0]
  - [halfTargetHeight, 50.0]
  - [buildKey, 559908232]
  - [dataGuid, 905cd1e3fbd014b3badd9f0076035b15]
  - [managedSpriteCollection, 0]
  - [hasPlatformData, 0]
  - - spriteCollectionPlatforms
    - []
  - - spriteCollectionPlatformGUIDs
    - []

The data for the Texture2D itself looks like this:

!unitypack:Texture2D
image data: !!binary |
  <image data>
m_ColorSpace: 1
m_CompleteImageSize: 4194304
m_Height: 2048
m_ImageCount: 1
m_IsReadable: false
m_LightmapFormat: 0
m_MipCount: 1
m_Name: Cards-1
m_ReadAllowed: true
m_StreamData: !unitypack:StreamingInfo {offset: 0, path: '', size: 0}
m_TextureDimension: 2
m_TextureFormat: 12
m_TextureSettings: !!python/object/apply:collections.OrderedDict
- - [m_FilterMode, 1]
  - [m_Aniso, 1]
  - [m_MipBias, 0.0]
  - [m_WrapMode, 1]
m_Width: 2048

However, I don't see how the Texture2D asset points to the spriteDefinitions or vice versa.

andyearnshaw avatar Oct 06 '17 13:10 andyearnshaw

This would need to be explored more closely then. If you can figure it out, I recommend you write a script that uses unitypack to extract the stripes, then we can figure out if we can upstream it :)

jleclanche avatar Oct 06 '17 13:10 jleclanche

OK, cool - I'll keep looking into it and see if I can rope some others into helping. Thanks!

andyearnshaw avatar Oct 06 '17 13:10 andyearnshaw

I realised that I missed some of the data off at the bottom of the sprite definitions object. I've added it in now and it clearly states which Texture the items map to in that data at the bottom:

  - - textures
    - - !PPtr [0, 8453760564669429443]

That PPtr references the Texture2D's path_id.

andyearnshaw avatar Oct 06 '17 14:10 andyearnshaw