Author Topic: UIButton Sprites prefab serialization bug  (Read 7818 times)

pivnix

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
UIButton Sprites prefab serialization bug
« on: April 01, 2014, 08:54:36 AM »
Greetings,
i faced a weird issue with UIButton Normal sprite. If my UIButton is in prefab and i change Normal sprite to any other sprite and then click Apply in prefab bar - it undo my sprite change and force Normal sprite to sprite it was before.
Same behaviour if changing sprite directly in UISprite that linked to Tween Target.
With Hover/Pressed/Disabled sprites all works fine.

Occur even on example NGUI scenes.
NGUI v. 3.5.5
Unity3d v. 4.3.4f1

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #1 on: April 01, 2014, 10:43:18 AM »
Drag the prefab into the scene, make changes there, then hit Apply.

ntopia

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #2 on: April 03, 2014, 06:58:02 AM »
Drag the prefab into the scene, make changes there, then hit Apply.

It still happens.

I'm using NGUI 3.5.5 & Unity 4.3.4f1

ntopia

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #3 on: April 03, 2014, 07:38:35 AM »
I found something.

For searching reason, I set 'Asset Serialization' to 'Force Text' in Editor Settings.

In prefab file, mSpriteName was correct value.
In scene file, mSpriteName was wrong value.

I don't know why...

hubert689

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #4 on: April 10, 2014, 09:56:49 AM »
For me the switch to 'Force Text' didn't work. But I found a work around.
This issue seems to happen only when I have a UISprite with a UIButton component on it. If I try to change the sprite to another one, the issue occurs.
But if I delete the UIButton component then change the sprite and then add back a UIButton script on it, the error doesn't occur.




BeShifty

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 7
  • Posts: 52
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #5 on: April 25, 2014, 03:56:47 PM »
This is still occurring for me with NGUI 3.5.8

Steps:

Drag a prefab with a UIButton into the scene
Change the normal sprite in UIButton to something else
Hit apply

Sprite is reverted

Edit: Note that the prefab will actually update correctly, but the instance is still reverting itself. Pretty critical for workflow.
« Last Edit: April 25, 2014, 04:10:00 PM by BeShifty »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #6 on: April 26, 2014, 02:36:45 PM »
Not getting reverted on my end. Works as expected. Make sure the 'mNormalSprite' and 'mSprite' are both marked as NonSerialized in your UIButton:
  1.         // Cached value
  2.         [System.NonSerialized] string mNormalSprite;
  3.         [System.NonSerialized] UISprite mSprite;

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #7 on: April 28, 2014, 11:10:15 AM »
I'm seeing this bug too. My current workaround it is to remove the button component, change the sprite, then add a new button script and set it back up. Not fun. It seems to happen 100% of the time in my current project.

Steps to reproduce:
Create a UISprite, set it to something (I'm using a sliced sprite)
Prefab the object
Add a UIButton (button and sprite are on the same object)
Set the UIButton target to the sprite (Don't bother changing hover
Apply changes, save scene
Change either the button normal sprite, or the sprite itself
Apply changes
Notice the sprite gets reverted the to initial sprite that was set when the UIButton component was added.

I checked the UIButton script as you suggested, the two mentioned fields were private, so they shouldn't be serialized. I added the NonSerialized attribute to be sure and the issue still occurred.

Additional info, in case it helps:
NGUI version: 3.5.5
Version Control: Visible Meta Files
Asset Serialization: Force Text

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #8 on: April 29, 2014, 02:42:21 PM »
Quote
the two mentioned fields were private, so they shouldn't be serialized
Tell that to Unity. It serializes them regardless. Even private fields. But only for prefabs. I think it's a Unity bug.

In your case, I suggest updating to 3.5.8. 3.5.5 has other issues where [NonSerialized] is not included on some fields.

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #9 on: April 29, 2014, 02:54:26 PM »
Strange. So, do you know if this particular issue if fixed in 3.5.8? As I said, adding [NonSerialized] didn't actually fix it on my end.

Unfortunately, upgrading isn't an option right now. Too close to launch.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #10 on: April 30, 2014, 06:14:00 PM »
All I know is that it resolved similar issues that were brought up recently.

gyd

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 87
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #11 on: May 22, 2014, 05:08:56 AM »
the issue happened in my game too.

here is the FIX step:
1. drag the prefab to scene view to Instantiate it
2. disable UIButton script
3. set the new values of UIButton
4. apply prefab
5. DEL the instance of prefab in scene
6. drag the prefab to scene view to Instantiate it
7. enable UIButton script
8. apply again, now the prefab is work as new values

here is the REASON of the issue, and i think it's a BUG of NGUI, instead of Unity:
*[ExecuteInEditMode]
*mNormalSprite caches in OnInit()
*SetState(State.Normal ) when OnEnable();

because OnInit will be not called again once mInitDone is set true, the old value will be cached until re-Instantiate the prefab again.

  1. UIButton.cs
  2. //line 116-120
  3.                 get
  4.                 {
  5. +#if UNITY_EDITOR
  6. +           if (!Application.isPlaying) mInitDone = false;
  7. +#endif
  8.                         if (!mInitDone) OnInit();
  9.                         return mNormalSprite;
  10.                 }
  11. // line 215
  12. -           case State.Normal: SetSprite(mNormalSprite); break;
  13. +           case State.Normal: SetSprite(normalSprite); break;
  14. //line
  15.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #12 on: May 22, 2014, 03:54:30 PM »
Thanks, but I think the better approach would be to reset 'mInitDone' to 'false' in UIButtonColor's OnEnable:
  1.         protected virtual void OnEnable ()
  2.         {
  3. #if UNITY_EDITOR
  4.                 if (!Application.isPlaying)
  5.                 {
  6.                         mInitDone = false; // <-- this right here
  7.                         return;
  8.                 }
  9. #endif
  10.                 if (mInitDone) OnHover(UICamera.IsHighlighted(gameObject));
  11.  
  12.                 if (UICamera.currentTouch != null)
  13.                 {
  14.                         if (UICamera.currentTouch.pressed == gameObject) OnPress(true);
  15.                         else if (UICamera.currentTouch.current == gameObject) OnHover(true);
  16.                 }
  17.         }

gyd

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 87
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #13 on: May 23, 2014, 12:52:59 AM »
Thanks, but I think the better approach would be to reset 'mInitDone' to 'false' in UIButtonColor's OnEnable:
  1.         protected virtual void OnEnable ()
  2.         {
  3. #if UNITY_EDITOR
  4.                 if (!Application.isPlaying)
  5.                 {
  6.                         mInitDone = false; // <-- this right here
  7.                         return;
  8.                 }
  9. #endif
  10.                 if (mInitDone) OnHover(UICamera.IsHighlighted(gameObject));
  11.  
  12.                 if (UICamera.currentTouch != null)
  13.                 {
  14.                         if (UICamera.currentTouch.pressed == gameObject) OnPress(true);
  15.                         else if (UICamera.currentTouch.current == gameObject) OnHover(true);
  16.                 }
  17.         }

but UIButtonColor's OnEnable() is not called by UIButton's override..

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sprites prefab serialization bug
« Reply #14 on: May 23, 2014, 03:56:45 PM »
Good point. Well, add the same
  1. #if UNITY_EDITOR
  2.                 if (!Application.isPlaying)
  3.                 {
  4.                         mInitDone = false;
  5.                         return;
  6.                 }
  7. #endif
...to the top of UIButton's OnEnable as well.