Hello Guest

Author Topic: Fastest way to load PNG assets?  (Read 8345 times)

David Kalina

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 26
    • View Profile
Fastest way to load PNG assets?
« on: May 27, 2015, 04:55:25 am »
Hello,

Our load times on older mobile devices (e.g. iPad2) are dismal.  We load quite a bit of 2D art, all of which is stored in PNG Atlas style sprite collections.

Anyway, after doing some research on the interwebs, I discovered that the standard 2DToolkit method for loading PNGs (calling LoadImage on a TextAsset's bytes followed by Apply) is quite a bit slower than loading the asset using Resources.Load.

I hacked together a test -- put one of my 1024x2048 .png assets into Resources and loaded it both ways on an iPad 2 -- and lo and behold, the LoadImage / Apply method timed at about ~0.75 seconds vs ~0.13 seconds for using Resources.Load.

I'm curious if there's some reason I shouldn't just be spitting built sprite collection .PNGs into /Resources with unique names, saving those names in the tk2dSpriteCollectionData, and then loading them via Resources.Load instead. 

Additionally, this isn't accounting for the benefit of not having to load those PNG TextAssets just to create the actual Texture2Ds, which Unity automagically does if the Sprite Collection is referenced in a level anywhere.  It certainly seems like it warrants a further experiment on my end, unless there is some clear reason this won't work that I am missing. 

So is there some clear reason this won't work that I'm missing?  :)

Thanks in advance!
David

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Fastest way to load PNG assets?
« Reply #1 on: May 27, 2015, 01:03:05 pm »
I don't see why this wouldn't work - as long as you make sure the texture is loaded in time for the sprite collection it should work fine.

39thstreet

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 77
    • View Profile
Re: Fastest way to load PNG assets?
« Reply #2 on: June 26, 2015, 08:48:36 pm »
Additionally, this isn't accounting for the benefit of not having to load those PNG TextAssets just to create the actual Texture2Ds, which Unity automagically does if the Sprite Collection is referenced in a level anywhere.  It certainly seems like it warrants a further experiment on my end, unless there is some clear reason this won't work that I am missing. 

Thanks for this thread as we've been looking into some slowness with our PNG loads!

We're trying to use this method to improve load times on our png assets, but when we try to load the .png.bytes files via resources.load we aren't able to cast them as a texture2D, is there another step to this we are missing?

Did you end up doing this, and if so, do you have any implementation tips?

TIA!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Fastest way to load PNG assets?
« Reply #3 on: June 28, 2015, 01:41:10 pm »
You can't, as far as I know. The .bytes files is just a byte stream.

David Kalina

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Fastest way to load PNG assets?
« Reply #4 on: June 29, 2015, 06:15:43 pm »
Yes, I had the knucklehead realization that putting them into Resources as .png files (rather than .png.bytes) has the same effect as using Unity textures in the first place -- totally uncompressed textures in the package.  So yes, loading becomes radically faster...  and your 2.6MB 2048x2048 becomes 16MB on disk.  Fuuuuuuck Unity and its lack of native PNG support, seriously.

I did continue to go in this direction though, choosing to put all PNG Atlases in Resources as .png.bytes and loading them as TextAssets before upload.  This ultimately doesn't *directly* help with load times (still has the massive spike associated with converting a PNG and uploading it to the graphics hardware), but there were positive indirect benefits:

- Our levels point at SCs that are not needed at load-time, this allows us to only load the TextAssets and convert them to Texture2Ds when we actually need them.
- ...which reduces memory footprint and level load times.
- ...and allows us to actively manage SC memory while travelling between sections of levels that have different SC needs, keeping max footprint down.

All of this for the love of the iPad 2 :(

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Fastest way to load PNG assets?
« Reply #5 on: June 29, 2015, 07:10:58 pm »
I'm just gonna throw this out there but depending on the amount of time you have available for the project, you could load the texture (png) yourself, decode and fill the Texture2D ALL from a native plugin, without crossing any language boundaries whatsoever... Unity now has the native textureptr exposed which is great for stuff like this. Its still a fair bit of work to implement though.

Sickwitit

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Fastest way to load PNG assets?
« Reply #6 on: July 04, 2015, 06:29:22 pm »
Why not just create an object pool, load all your png assets into the object pool when the game starts, deactivate the objects , then just activate those objects when you need them, and deactivate when you don't, instead of trying to accessing your atlas constantly during runtime.

This may be trolling, but thought i'd give the quick fix suggestion.