Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: QuantumMechanic on January 04, 2013, 08:29:19 PM

Title: UI Sprite Dynamically setting Atlas and SpriteName not working...
Post by: QuantumMechanic on January 04, 2013, 08:29:19 PM
When attempting to dynamically set the Atlas and SpriteName of a UISprite it doesn't seem to be working.  Setting the atlas seems to work, but setting the spriteName seems to fail.

Sample Code:

  1. private var m_DynamicGameObject:GameObject;
  2. private var m_Sprite:UISprite;
  3. public var m_Panel:UIPanel;
  4. public var m_Atlas:UIAtlas;
  5.  
  6. function Start () {
  7.     m_DynamicGameObject = new GameObject();
  8.     m_Sprite = m_DynamicGameObject.AddComponent("UISprite");
  9.  
  10.     m_DynamicGameObject.transform.parent = m_Panel.transform;
  11.  
  12.     m_Sprite.atlas = m_Atlas;
  13.     m_Sprite.spriteName = "in_golfball_bu_no"; 
  14.  
  15. }
  16.  

I Suspect the problem is in line 110 of UISprite.  UpdateUVs is only called if mSprite is not null, but mSprite is set to null two lines above.  Removing the if check and just calling UpdateUVs seems to correct the problem.  Code snippet from UISprite below for easy reference.

  1.                 set
  2.                 {
  3.                         if (string.IsNullOrEmpty(value))
  4.                         {
  5.                                 // If the sprite name hasn't been set yet, no need to do anything
  6.                                 if (string.IsNullOrEmpty(mSpriteName)) return;
  7.  
  8.                                 // Clear the sprite name and the sprite reference
  9.                                 mSpriteName = "";
  10.                                 mSprite = null;
  11.                                 mChanged = true;
  12.                         }
  13.                         else if (mSpriteName != value)
  14.                         {
  15.                                 // If the sprite name changes, the sprite reference should also be updated
  16.                                 mSpriteName = value;
  17.                                 mSprite = null;
  18.                                 mChanged = true;
  19.                                 if (mSprite != null) UpdateUVs(true);
  20.                         }
  21.                 }
  22.  
Title: Re: UI Sprite Dynamically setting Atlas and SpriteName not working...
Post by: ArenMook on January 04, 2013, 08:33:56 PM
  1. UISprite sprite = NGUITools.AddWidget<UISprite>(m_Panel.gameObject);
  2. sprite.atlas = m_Atlas;
  3. sprite.spriteName = "in_golfball_bu_no";

Your example doesn't set the layers properly as you are not using NGUITools.AddWidget or NGUITools.AddChild.
Title: Re: UI Sprite Dynamically setting Atlas and SpriteName not working...
Post by: ArenMook on January 04, 2013, 08:40:12 PM
P.S. But yes I do see what you're talking about. I'll fix it, thanks.