Hello Guest

Author Topic: platform sprites still loading into ram  (Read 5486 times)

sebastian

  • Newbie
  • *
  • Posts: 17
    • View Profile
platform sprites still loading into ram
« on: August 22, 2014, 01:56:29 am »
Thanks for all of your help Unikron.  Unfortunately have another question for you...

I've been trying to implement platforms on tk2d but my app still crashes cuz of memory pressure at startup on my nemesis, the iphone4s.

My platform setup and @2x and @1x textures work fine in the Unity3d editor. Textures switch great.

However, since the iphone4s is still crashing on me cuz of memory pressure when building in Xcode, I am fairly certain that somehow my larger textures must be getting loaded into RAM at startup-- despite the platform setting.

I suspect this is case because:

A) before implementing platforms, as a test I manually changed all of my tk2d atlas compression to 16bit from 32 and it loaded fine on the iphone4s

B)  i then implemented platforms which by my calculations should be much much smaller than simply changing from 32 to 16 (in fact my 2x and 1x textures are ALSO set to 16bits as well as being physically smaller)

C)  I'm certain I'm switching the tk2d platform before a single sprite loads.  I even went so far as to add a blank scene to the very start of my app that ONLY has: tk2dSystem.CurrentPlatform = "1x";
My debug verifies that I am indeed using the 1x platform at the very start of loading.

However I'm still getting memory crashes on startup.

Could this be because I *started* my sprites with a default size of 4x?

So maybe the default 4x has to load initially before then switching down to 1x?

I'd clarify that while some of my tk2d sprites are being loaded as prefabs, some are just sitting in my scene #2 hierarchy.  So I'm assuming maybe those 4x textures load and hit ram somehow before getting a chance to switch to 1x?

Thanks very much for your great support and ongoing help!!

Sebastian
« Last Edit: August 22, 2014, 02:03:37 am by sebastian »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: platform sprites still loading into ram
« Reply #1 on: August 22, 2014, 07:54:53 pm »
There was a bug in some version of unity where unity stored a reference to something it shouldn't have. I reported this AGES ago, and I thought it'd been fixed, but lets find out for sure. Write a bit of code to verify this - On Start in your scene, use Resources.FindObjectsOfTypeAll(typeof(Texture2D)) and print all the names. You should not see any @4x platform sprites there. No use guessing if its loaded, you can find out for certain.

If it is, try setting your platform in the unity editor to 1x, then build it. See if that improves things. If so, that bug hasn't been fixed yet  :o I do have a few workarounds for this, so find out first if it is that bug.

sebastian

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: platform sprites still loading into ram
« Reply #2 on: August 22, 2014, 09:21:29 pm »
Ah okay-- that's very helpful!

What I'm seeing is that actually, despite what platform I set in startup, and indeed in a pre startup blank scene, all THREE texture sizes are actually showing up in debug using Resources.Find per your suggestion-- @1x, @2x and @4x.

So it looks like all three versions are hitting ram.  I tested setting different platforms at startup, and also tested changing the tk2d platform in editor preferences with the same result.

Definitely explains my crash.  Btw I'm using Unity 4.5.2f1

Thanks for your help!!
Sebastian

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: platform sprites still loading into ram
« Reply #3 on: August 22, 2014, 09:27:35 pm »
Are you testing a built version? You're likely to get random results in the editor. Its impossible for all 3 to be loaded, as they are loaded explicitly... The bug would explain 2 of them, but not 3....

sebastian

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: platform sprites still loading into ram
« Reply #4 on: August 22, 2014, 09:41:45 pm »
Oh okay sorry I misunderstood.  Yes I was testing in editor.  I'll build now and report back..

Sorry for that!!
Thank you!!

sebastian

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: platform sprites still loading into ram
« Reply #5 on: August 22, 2014, 11:39:58 pm »
It worked!!!

Hooray!  So just as you suggested, when I built in Xcode, ONLY the platform specific sprites showed up :).

Loads fine now on the 4s!

So the question is-- what the heck was I doing wrong before??

I think there are four possibilities?

1)  There is a *chance* that maybe I only tested 4s with 2x.  I can't imagine why I would only do that-- but my memory's a little fuzzy on it now.  But 2x makes it crash.  Only going down to 1x gets it to run.  So if I initially was stupid and didn't check 1x that would explain the crash.  I think I did check 1x though???  I can't remember. :(

2)  In my successful test, I built 1x and it worked on 4s.  I then switched to 2x and it crashed.  I switched back to 1x and it crashed again!  I then went into tk2d preferences and changed the default editor platform to 1x.  This time at 1x it worked again.  Tried again to make sure and it's running fine.  I'm not sure if changing the default platform to 1x actually helped-- in my experience the 4s has so little ram that sometimes it just chokes randomly if you've pushed the device too much without a restart.  So maybe switching tk2d preferences to default 1x had an effect-- though I suspect maybe the phone just glitched out.

3)  The only other real thing I did is when you asked me to print the textures showing up, I noticed all the platform specific atlases were defaulting to the same name: atlas0.  1x, 2x and 4x sprites were all called atlas0.  So I manually changed the names of those atlases so I could see what was happening in debug.  (i.e. I changed to myatlas01_1x, myatlas01_2x, myatlas01_4x).  I have no idea whether that had an impact or not.  Could tk2d have confused the atlases if they were all called atlas0??  I doubt it cuz it looks like it's all folder based.  But anyway, that's something I did and now it's working.

4)  Most likely-- I did something stupid and I'm not sure what. lol  I am prone to doing dumb stuff-- so maybe there was some script of mine that I screwed up on and subsequently got rid of between crash and success?  I am certain that the 4s was originally crashing due to memory pressure.  that was the Xcode error.  Anyhow, it is definitely possible  that some dumb script of mine was responsible for the error.

Anyway, it's all working now!  Sucks that I'm still a little fuzzy as to why it's working now and wasn't before?  But mainly I'm grateful to have it running!!

Thank you so much, Unikron for your awesome software and support!!  I'm heading over to the asset store now to amend my already very positive review to make it more positive.

Thanks again.  I'm super grateful for all of your help.

Sebastian
(funAsylum on asset store)
« Last Edit: August 22, 2014, 11:47:42 pm by sebastian »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: platform sprites still loading into ram
« Reply #6 on: August 23, 2014, 11:04:17 am »
I have no idea, but try to verify if that bug still exists. Switch to 4x, build, then run on your iphone, see if it crashes. If it does, thats the bug, otherwise I have no idea :)

sebastian

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: platform sprites still loading into ram
« Reply #7 on: August 23, 2014, 04:30:11 pm »
Hey so I think I finally figured out what was going on...

In my game I actually load three scenes.  Scene 1 is just a blank scene with a single change tk2d platforms command.  Scene 2 is a preloader with a simple load bar.  And Scene 3 is the game with all the assets.

I realized that I had actually put some prefabs directly in the preloader hierarchy (i.e. Scene 2) that I had turned off and completely forgotten about.  These preload elements had references to quite a few sprite sheets.  These were the culprits!

I found that the sprite sheets for just those forgotten elements were double loading-- i.e. would load both @4x and @1x elements.

A few things I learned just in case anyone else is reading and potentially runs into a similar issue:

1.  Even though I changed tk2d platforms in an entirely blank scene before switching to the preload scene, tk2d did not switch platforms QUICK ENOUGH to avoid the instant load of scene2 and the hidden assets hitting RAM.  I'm sure if I had waited even .25f seconds it would have been fine, but scene2 loaded right away after scene1, and so the switch didn't happen in time.

2.  The default tk2d platform set in editor is important because it is the INITIAL STARTING platform that's used before any manual switch happens.  So even though I had a blank scene switching platforms to 1x, tk2d started up in 4x and the assets I'd forgotten about in scene 2 started in 4x before getting a chance to switch

3.  Hidden assets could be the problem:  One reason I was stumped for so long is because I had this blank scene at startup switching tk2d platforms.  I was under the impression that I was switching platforms way before any assets appeared in my game (i.e. before the preloader even).  I had forgotten all about some offed prefabs in the preloader hierarchy (my game has 72 levels and a ton of stuff flying around-- so I forgive myself lol).  Anyway, I didn't consider that any assets could have hit RAM before my initial switch platform command cuz I wasn't aware of those assets!  Also, even switching to an entirely new scene didn't create enough of a delay between tk2d platform switch and assets sitting in hierarchy of the next scene for the platform to switch in time.  This, of course, was the point of my preloader-- to stagger the load.  But the forgotten assets didn't benefit from this time/load management

Anyway-- thanks very much again, Unikron!  Everything is working great now!
« Last Edit: August 23, 2014, 04:40:09 pm by sebastian »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: platform sprites still loading into ram
« Reply #8 on: August 23, 2014, 07:24:16 pm »
#2 is likely due to the unity bug I mentioned. It is still open in the unity bug reporter.
http://issuetracker.unity3d.com/issues/hideflags-dot-hideanddontsave-not-respected-when-executeineditmode-is-on