Author Topic: Bug when using UISprite.sprite = ... to change sprite dynamically  (Read 4453 times)

David

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Hi, I don't know if it's the correct place to write a bug report but here it is :

I bought NGUI some days ago (thanks by the way, it's fantastic :)) ) and I just had a problem when trying to change dynamically the sprite used by a UISprite.

Changing sprite with UISprite.spriteName = ... works but UISprite.sprite = ... doesn't (the sprite image is modified then returns to its last value as soon as update is called).

The problems seems to come from the fact mSpriteName isn't updated in UISprite.sprite's setter, if I add mSpriteName = mSprite.name in UISprite.cs at line 146 it works as expected but I don't know if it has other side effects.

I hope it can be usefull.
« Last Edit: December 28, 2012, 09:40:45 AM by David »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Bug when using UISprite.sprite = ... to change sprite dynamically
« Reply #1 on: December 28, 2012, 10:41:43 AM »
Why would you change the sprite itself?

David

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Bug when using UISprite.sprite = ... to change sprite dynamically
« Reply #2 on: December 28, 2012, 10:49:13 AM »
Not sure to understand your question. Why changing UISprite.sprite instead of UISprite.spriteName ? Because we can ;D No, I'm just joking  ;), the UISprite.sprite property was the first one I found and I tried to use it before finding that I can change UISprite.spriteName. It seems that I'm not the only one that tried (and failed to use it) this way if you search in the forum.

Or was it "Why I try to change a UISprite dynamically" ? In fact I have 6 action icons around my character and these icons depends of what the character has in his inventory, so I needed to change them dynamically. I thought it would be the best way to do it, or is there a better way ?
« Last Edit: December 28, 2012, 10:50:55 AM by David »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug when using UISprite.sprite = ... to change sprite dynamically
« Reply #3 on: December 28, 2012, 12:11:28 PM »
'sprite' field should never be used. Use spriteName and only spriteName when setting a sprite. Setting a 'sprite' is for internal use. Unfortunately there is no good way of hiding it.

David

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Bug when using UISprite.sprite = ... to change sprite dynamically
« Reply #4 on: December 31, 2012, 02:04:20 AM »
Thanks for your answer, maybe a comment in the code would help those who'll try to do the same way as I did ;D