Author Topic: Losing sprite references using Atlas reference  (Read 14598 times)

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Losing sprite references using Atlas reference
« on: February 15, 2013, 05:31:54 PM »
I have written an editor script to set all my Atlas References to null before building to devices to ensure that it won't load the wrong atlas that I had set in the editor, then switch to the right one.  If I don't do this, my app will crash on some lower memory devices, and of course takes longer to load on others.

The problem is, that if the atlas reference is set to null, any sprites pointing to that atlas reference can lose their sprite setting in the editor if I am not super careful.  This is quite troubling when you have 100 objects that all lost their sprites and have to go reset them each to what they are supposed to be, if I didn't have a recent .scene backup.  I have had to do this many times now.  Is there anything I am doing wrong or possibly a patch that would make NGUI not reset the sprite reference even when the atlas reference is null?

Much appreciate some help on this, as it's driving me nuts.  ;D

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #1 on: February 15, 2013, 05:43:57 PM »
To add to this, even more troubling observation:

If I set an Atlas Reference to null at run-time, then load it back up later, I lose my sprite references as well.  So is there any way to properly load/unload Atlas references without causing this issue? Or some fix I could implement?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Losing sprite references using Atlas reference
« Reply #2 on: February 16, 2013, 02:03:09 AM »
There is no reason why you would lose sprite references. Sprites references are always temporary, and only sprite names and atlas references actually get saved. I'm curious about your choice of wording: "Atlas References". There should be only one reference atlas, and all your widgets should be using it. Thus changing the atlas reference should happen only in one place -- on the reference atlas, and never on widgets themselves.

Make sure names of your sprites match exactly in all atlases as well. They are case sensitive, so "Hello World" is not the same as "hello world".

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #3 on: February 16, 2013, 02:17:00 AM »
I am not typing in any sprite names.  This is all being done in the editor, so there is no room for typos.  When this issue occurs, many of the widgets seem to default to the first sprite on the atlas. 

The reason for the term Atlas Reference(s), is because I have 4 different Reference Atlases I am using, since I cannot fit all my assets onto one atlas.  I am using references so I can at run-time switch between SD, HD, 4X.  This issue happens whether I have 1 or 10 atlases though, so that's not relevant.  I am not changing anything on the widgets themselves, but many widgets will end up defaulting to the first sprite on the sheet in certain scenarios when the Reference Atlas is set to null.

Edit: I should add that when I said sprite references earlier, I was meaning the sprite name that each widget is set to.
« Last Edit: February 16, 2013, 02:22:06 AM by FizzPow »

Game Whiz

  • Guest
Re: Losing sprite references using Atlas reference
« Reply #4 on: February 16, 2013, 06:20:32 AM »
I'm now seeing this behaviour. If in a dummy atlas, atlas.replacement is set to null, all sprite references are lost. This didn't happen previously (in 2.3 it was fine).

Game Whiz

  • Guest
Re: Losing sprite references using Atlas reference
« Reply #5 on: February 16, 2013, 07:03:44 AM »
It's a bug in this latest version of NGUI. I corrected it by commenting out the line that sets the sprite name to null, if the sprite is null, in UISprite.cs:

public UIAtlas.Sprite sprite
   {
      get
      {
         if (!mSpriteSet) mSprite = null;

         if (mSprite == null && mAtlas != null)
         {
            if (!string.IsNullOrEmpty(mSpriteName))
            {
               sprite = mAtlas.GetSprite(mSpriteName);
            }

            if (mSprite == null && mAtlas.spriteList.Count > 0)
            {
               sprite = mAtlas.spriteList[0];
               mSpriteName = mSprite.name;
            }

            // If the sprite has been set, update the material
            if (mSprite != null) material = mAtlas.spriteMaterial;
         }
         return mSprite;
      }
      set
      {
         mChanged = true;
         mSprite = value;
         mSpriteSet = true;

         if (mSprite != null)
         {
            mSpriteName = mSprite.name;

            if (mAtlas != null)
            {
               material = mAtlas.spriteMaterial;
               UpdateUVs(true);
            }
         }
         else
         {               
            //mSpriteName = "";
            material = null;
         }
      }
   }

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #6 on: February 16, 2013, 01:42:34 PM »
Hmm, this is causing exception errors for me, and also still losing sprite names in some scenarios.

Game Whiz

  • Guest
Re: Losing sprite references using Atlas reference
« Reply #7 on: February 16, 2013, 01:49:12 PM »
Without knowing the exceptions and the scenarios it's hard for me to offer any ideas :)

Anyway, this was a quick hack, after 15m debugging. It's working in a stock version of 2.33b. I had to redo one of my atlases, but everything is ok now.

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #8 on: February 16, 2013, 01:55:51 PM »
I am guessing I need to create a repro project to share when I have some time later.  It feels like there are about 100 scenarios where things still break for me.  That exception error happens on the get {} when the atlas is set to null.  I get one for every active sprite.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Losing sprite references using Atlas reference
« Reply #9 on: February 16, 2013, 01:58:20 PM »
I pushed 2.3.3c with the fix Game Whiz posted.

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #10 on: February 16, 2013, 01:59:10 PM »
I pushed 2.3.3c with the fix Game Whiz posted.

Did you do anything else? Because that's definitely not a fix as I mentioned and causes lots of exception errors?

Game Whiz

  • Guest
Re: Losing sprite references using Atlas reference
« Reply #11 on: February 16, 2013, 02:00:05 PM »
Try moving your atlas switching code up in the scripting order. That way you'll have an atlas whenever you're trying to get a sprite.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Losing sprite references using Atlas reference
« Reply #12 on: February 16, 2013, 02:00:59 PM »
Alright well I have to run out. Please debug it properly and post a proper fix that will cause your issues to go away because I can't debug things that don't cause any issues for me. I'll check again in a few hours and put up whatever fix you come up with.

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #13 on: February 16, 2013, 02:02:11 PM »
Alright well I have to run out. Please debug it properly and post a proper fix that will cause your issues to go away because I can't debug things that don't cause any issues for me. I'll check again in a few hours and put up whatever fix you come up with.

I honestly am not sure how to fix it  =)  That's why I am posting here.  I can send you a repro project though?

FizzPow

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 83
    • View Profile
    • FizzPow Games
Re: Losing sprite references using Atlas reference
« Reply #14 on: February 16, 2013, 02:06:19 PM »
Try moving your atlas switching code up in the scripting order. That way you'll have an atlas whenever you're trying to get a sprite.

I can't have the references set to anything otherwise Unity will try to load whatever reference was set in the editor first, and then even with script execution at top, it will then re-load a different atlas at run-time.  This breaks on low memory devices that try to load HD assets and crash from out of memory.  One super lame fix =) is to always set my atlases to the lowest quality in the editor, but then it would be wasting time loading low quality assets then re-loading the proper ones.

Also there are some atlases I don't want to load at all until they are needed and then unload them, which causes all kinds of issues as well with sprites pointing to a null reference atlas as stated earlier.