Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: boofcpw on May 13, 2014, 11:27:36 PM

Title: Sprites in prefab reverting
Post by: boofcpw on May 13, 2014, 11:27:36 PM
Hey,

Recently I have been experiencing an issue with buttons in a prefab.

Whenever I change the sprite that they use, upon applying the prefab, the sprite reverts back to what was originally in the prefab.
I haven't experienced this before, so I'm not sure if I'm doing something silly or there is an issue with the new version, but any help would be great!
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 13, 2014, 11:35:27 PM
So I have figured that this only happens to buttons with box colliders. If I remove the box collider then apply the prefab, the sprite does not revert. However, when I add a new box collider then apply, the sprite once again reverts. Im pretty stumped.
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 14, 2014, 06:22:58 AM
In what version of NGUI? This was addressed back in 3.5.8
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 14, 2014, 06:01:00 PM
Version 3.5.9
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 15, 2014, 12:06:59 AM
Alright, then I'm not sure what would be the case. The normal sprite is not saved in any way. It's marked as "NonSerialized" in UIButton.cs. Changing the normal sprite actually changes the current sprite's spriteName directly (UIButtonEditor.cs line 52). I don't see anything wrong on my end. Changing the sprite and hitting Apply does not revert in my case, with or without a collider. Is there a step-by-step repro case you can provide?
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 15, 2014, 03:21:57 AM
I created a completely new project and reproduced the issue. Here are the steps required:

new project
import ngui 3.5.9
import two texture
create atlas from two texture (initially created an error. different issue, but probably something to look at)
Add 2d ui to scene
save scene
create panel
create sprite (under panel)
set sprite to use atlas and sprite
add UI button script to sprite
add collider to sprite
save scene
run scene (button works)
Create a prefab
drag panel into prefab
copy sprite (with button on it)
change new buttons sprite
select panel
press apply
second button reverts back to original buttons sprite

Hope that helps
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 15, 2014, 03:27:32 AM
Just another note. Both sprites revert when changed. not just the copied one.
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 15, 2014, 11:57:55 AM
Reproduced, thanks. I will look into this.
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 15, 2014, 05:53:08 PM
Thanks a lot
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 16, 2014, 01:03:19 PM
Fixed in 3.6.0 btw.
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 16, 2014, 11:34:09 PM
Awesome!
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 19, 2014, 12:19:32 AM
Hey, just updated (3.6.0) and am still occasionally getting this issue.

Does the change you did fix buttons which had the issue previously, or does it only fix newly created ones?
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 19, 2014, 12:37:20 PM
It should fix everything. How often are you still seeing the issue, and is there any way to reproduce it consistently?
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 20, 2014, 12:36:29 AM
Every time I copy a button and change the sprite, it reverts upon pressing apply. This is with an existing project. I have not had a chance today to start a fresh project and get the exact steps required to reproduce the issue, but I cant imagine it would be too different.

Just to clarify, what I am currently doing is:

Copy button (consists of sprite, button, collider, button scale)
Change new buttons sprite
Apply prefab
New button reverts to old buttons sprite.

To be clear, I definitely did upgrade NGUI properly (nuke it from orbit), and am definitely running the latest version.

Ill try get a reproducible from a fresh project when I get the chance. Its a busy week  ;)
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 20, 2014, 12:53:29 AM
I just opened up the project I made to reproduce the issue last time, and upgraded NGUI to make sure that it fixed that issue, but that project is still having the same problem.

I created a new project, and began repeating the steps of the original bug, and found several things

1) not related, but still something that does happen pretty often. When creating an atlas, getting the error "MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it."

2) when I first created a sprite, the sprite name was by default set as a sprite that is in a completely different project (might not be a problem/related, but I found that interesting)

3) the same bug with the applying prefab/reverting sprite.

If you follow the steps I posted previously, you should still be getting the same problem.

Thanks!
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 20, 2014, 01:42:38 PM
Hmm... no matter what I try I can't seem to reproduce it on my end anymore.

1. New scene.
2. ALT+SHIFT+S to add a sprite.
3. Added UIButton to it.
4. Saved the sprite as a prefab.
5. Changed the sprite's "Normal" state on the UIButton, Apply the prefab. Changes stay.
6. Changed the sprite on the UISprite, Apply. Changes stay.
7. Selected the prefab itself, changed the sprite from there. Works as expected.

What are you doing differently?
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 20, 2014, 05:47:09 PM
Instead of adding the button to a prefab, try create a prefab from the buttons parent panel (as if it were a window prefab or something)

Then, try to copy the button, and change the new copied buttons sprite.

Apply the window prefab, and the second button should revert.

Basically
-Create panel
-Add button below panel
-Save panel as prefab
-Copy button
-Change new copied buttons sprite
-Apply panel prefab
boom!

I believe the act of copying the button is what breaks it. Perhaps there is some reference to the original hanging around?

Heres where it gets super weird. If you duplicate the panel prefab, after making a change and getting the sprite to revert itself on application of the panel prefab, the new panel prefab actually has the correct sprite!
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 20, 2014, 06:08:20 PM
I have created a project to help you reproduce the issue

https://www.dropbox.com/s/l9cdqancbls3hf4/NGUI_Button_Test_Two_No_NGUI.zip

The steps required are:
-open test scene
-import ngui
-in the scene hierarchy, copy the button in window
-change the new buttons sprite
-apply window
-(new button should revert)
-from this point, duplicate the window prefab in the project view
-add the new, duplicated window to the scene. It has the correct sprite (weird)
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 20, 2014, 10:54:43 PM
Thanks! It helped me track down the issue. Open up UIButtonColor (UIButton's base class) and add [System.NonSerialized] in front of all the protected variables:
  1.         [System.NonSerialized] protected Color mStartingColor;
  2.         [System.NonSerialized] protected Color mDefaultColor;
  3.         [System.NonSerialized] protected bool mInitDone = false;
  4.         [System.NonSerialized] protected UIWidget mWidget;
  5.         [System.NonSerialized] protected State mState = State.Normal;
...seems to fix it.
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 20, 2014, 11:14:40 PM
Hey, thanks

I have tried the fix in two project which had the issue and it seems to have not fixed either of them.

Its probably fair to blame super consistent solar radiation (or something) for this issue.
Title: Re: Sprites in prefab reverting
Post by: ArenMook on May 21, 2014, 11:07:58 AM
Still? Hm. I noticed the issue was popping up only when the UIButton was enabled. When disabled, everything would work as expected, which is what led me to make those changes. After doing so everything seemed to stick. Perhaps try adding [System.NonSerialized] to UISprite.mSpriteSet as well? We're trying to work around a bug in Unity here (private values should not be serialized, yet Unity serializes them for prefabs), and adding NonSerialized seems to be the only work-around.
Title: Re: Sprites in prefab reverting
Post by: boofcpw on May 24, 2014, 08:34:07 PM
I can confirm this no longer happens in the latest NGUI update. Thanks for that!
Title: Re: Sprites in prefab reverting
Post by: Nicki on May 25, 2014, 02:17:18 PM
Detail: Unity serializes it, because the debug inspector can see private members and shows their values. If they're nonserialized it does not. Sucks that it leads to weird caching errors like these.