Author Topic: setting Button.normalSprite doesn't take affect  (Read 9117 times)

bkinsey

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 30
    • View Profile
setting Button.normalSprite doesn't take affect
« on: November 11, 2014, 02:36:26 PM »
In my game I have several game objects with UIButton scripts, and they usually start off disabled. (i.e. lots of panels are not shown on game start)

When I set the button's normalSprite member and activate the game object, the new sprite is not drawn. I've also tried activating first and setting the sprite second, but it didn't change the result.  If I deactivate and re-activate the widget's, the correct sprite is now displayed.

Question: When setting a button's normalSprite, do I need to call any method to make the change final?  It looks like there is some kind of caching going on, or some reason when I set the sprite name, it doesn't immediately take effect.  I started noticing this in happening recently, maybe in 3.7.5 and 3.7.6.. my buttons are appearing with their default images, despite me setting them in code to different images.

bkinsey

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 30
    • View Profile
Re: setting Button.normalSprite doesn't take affect
« Reply #1 on: November 11, 2014, 02:42:07 PM »
On further investigation it looks like the sprite is not initialized, so setting it does nothing.

I had to force the buttons OnInit to be called, by calling the normalSprite 'get' method:

  1. // This is purely to force NGUI to initialize its internal sprite object.
  2. string x = mGuideVisitorButton.Button.normalSprite;
  3.  

Can you expose the OnInit method so that I can call it myself, or maybe figure out a more elegant solution? (We'd still want a safeguard preventing init from happening more than once)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: setting Button.normalSprite doesn't take affect
« Reply #2 on: November 12, 2014, 07:41:37 AM »
Simply add a call to it at the top of the 'set' just like there is at the top of 'get':
  1.         public string normalSprite
  2.         {
  3.                 get
  4.                 {
  5.                         if (!mInitDone) OnInit();
  6.                         return mNormalSprite;
  7.                 }
  8.                 set
  9.                 {
  10.                         if (!mInitDone) OnInit(); // <-- right here
  11.                         if (mSprite != null && !string.IsNullOrEmpty(mNormalSprite) && mNormalSprite == mSprite.spriteName)
  12.                         {
  13.                                 mNormalSprite = value;
  14.                                 SetSprite(value);
  15.                                 NGUITools.SetDirty(mSprite);
  16.                         }
  17.                         else
  18.                         {
  19.                                 mNormalSprite = value;
  20.                                 if (mState == State.Normal) SetSprite(value);
  21.                         }
  22.                 }
  23.         }

bkinsey

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 30
    • View Profile
Re: setting Button.normalSprite doesn't take affect
« Reply #3 on: November 12, 2014, 04:33:18 PM »
So simple, I like it!  Thanks, I will add this in.  I hope you add this in too.. so I get it in future releases.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: setting Button.normalSprite doesn't take affect
« Reply #4 on: November 14, 2014, 04:32:56 AM »
Of course. :)