Author Topic: UI2DSprite: Changing sprite2d property works only once  (Read 2505 times)

papatriz

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 10
    • View Profile
UI2DSprite: Changing sprite2d property works only once
« on: October 10, 2014, 08:31:29 AM »
Hello,

I've found a strange issue with UI2DSprite in latest version of NGUI with latest version of Unity3D Pro.

I have a few icons which can be active or locked. So I manage it with code below:
  1.         public UnityEngine.Sprite Active;
  2.         public UnityEngine.Sprite Locked;
  3.  
  4. void OnEnable ()
  5.         {
  6.  
  7.                 var key = "Lesson"+LessonNumber;
  8.  
  9.                 if (!PlayerPrefs.HasKey (key))
  10.                 {
  11.  
  12.                         if ( LessonNumber == 1 ) PlayerPrefs.SetString (key, "active");
  13.                          else
  14.                           PlayerPrefs.SetString (key, "locked");
  15.  
  16.                         PlayerPrefs.Save ();
  17.        
  18.                 }
  19.  
  20.                 LessonState = PlayerPrefs.GetString (key);
  21.  
  22.                 switch (LessonState)
  23.                 {
  24.        
  25.                         case "active":
  26.  
  27.                                 GetComponent<UI2DSprite> ().sprite2D = Active;
  28.                                 Debug.Log ("Sprite is "+GetComponent<UI2DSprite> ().sprite2D);
  29.                                 break;
  30.  
  31.                         case "locked":
  32.                        
  33.                                 GetComponent<UI2DSprite> ().sprite2D = Locked;
  34.  
  35.                         break;
  36.  
  37.                        
  38.                 }
  39.        
  40.         }
  41.  
  42.  

It worked fine before I've update NGUI for latest version.
Now it works in this manner:

Application starts.

All sprites set up in right way.

When state of some icons changed Debug.log shows me that sprite2D component changes to right value, but nothing happens on scene and also inspector still shows old value in UI2DSprite component.

Can you help me with this issue or give me some advice?

UPDATE:

Looks like the problem is in the UIButton script which attached to my icons.
Logs from UI2Dsprite constructor:

// Set sprite2d to proper value after call from my script:

SET sprite2D to Lesson_2 (UnityEngine.Sprite)
UnityEngine.Debug:Log(Object)
UI2DSprite:set_sprite2D(Sprite) (at Assets/NGUI/Scripts/UI/UI2DSprite.cs:49)
LessonIcon_Control:OnEnable() (at Assets/Scripts/LessonIcon_Control.cs:44)
UnityEngine.GameObject:SetActive(Boolean)

//debug.log from my script, shows right value for sprite2d

Sprite is Lesson_2 (UnityEngine.Sprite)
UnityEngine.Debug:Log(Object)
LessonIcon_Control:OnEnable() (at Assets/Scripts/LessonIcon_Control.cs:47)

//OOPS! UIButton revert sprite2d value to previos :

SET sprite2D to Lesson_2_locked (UnityEngine.Sprite)
UnityEngine.Debug:Log(Object)
UI2DSprite:set_sprite2D(Sprite) (at Assets/NGUI/Scripts/UI/UI2DSprite.cs:49)
UIButton:SetSprite(Sprite) (at Assets/NGUI/Scripts/Interaction/UIButton.cs:304)
UIButton:SetState(State, Boolean) (at Assets/NGUI/Scripts/Interaction/UIButton.cs:275)
UIButton:OnEnable() (at Assets/NGUI/Scripts/Interaction/UIButton.cs:213)

Removing UIButton component helps. Now I will try to investigate what wrong wuth UIButton cause I would like to continue using it.
« Last Edit: October 10, 2014, 09:31:16 AM by papatriz »

papatriz

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: UI2DSprite: Changing sprite2d property works only once
« Reply #1 on: October 10, 2014, 10:40:55 AM »
FINAL UPDATE:

Yes, this issue was due to UIButton script.

I fix it by adding OnInit call to OnEnable method:
  1. protected override void OnEnable ()
  2.         {
  3. #if UNITY_EDITOR
  4.                 if (!Application.isPlaying)
  5.                 {
  6.                         mInitDone = false;
  7.                         return;
  8.                 }
  9. #endif
  10.                 OnInit (); // <---
  11.  
  12.                 if (isEnabled)
  13.                 {
  14.                         if (mInitDone)
  15.                         {
  16.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UI2DSprite: Changing sprite2d property works only once
« Reply #2 on: October 11, 2014, 04:15:07 AM »
OnEnable shouldn't call OnInit, as OnInit should only be done once in Awake.

Instead of changing UI2DSprite's value you should have been changing UIButton.normalSprite2D, and everything would have worked.