Author Topic: Weird render bug after changing a sprites panel from a disabled one.  (Read 3483 times)

gerardo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
Using NGUI 3.6.8

Hi. I would really appreciate if you looked into this issue and maybe help us find a solution.
Basically we get the bug when we disable a sprite's current panel and then switch it to another one in the same function call.
The sprite seems to grow visually even though its size remains the same.

Initial selection.

Several transitions later...


Stripped down code that reproduces the bug.
  1. public class FocusChanger : MonoBehaviour {
  2.        
  3.         public UISprite FocusIndicator; //Red sprite (sliced). Initial parent is UIRoot
  4.         public UIWidget focusLeft; // left zone. Under "Panel"
  5.         public UIWidget focusRight; // right zone. Under "OtherPanel"
  6.         public UIPanel parentPanel; // "OtherPanel"
  7.  
  8.         bool leftSelected = false;
  9.  
  10.         void Start()
  11.         {
  12.                 ChangeFocus(focusLeft);
  13.                 leftSelected = true;
  14.         }
  15.  
  16.         void ChangeFocus(UIWidget focusZone)
  17.         {
  18.                 //reparent
  19.                 FocusIndicator.transform.parent = focusZone.transform.parent;
  20.                 FocusIndicator.transform.localPosition = focusZone.transform.localPosition;
  21.                 if(FocusIndicator.panel != focusZone.panel)
  22.                 {
  23.                         if(FocusIndicator.panel != null)
  24.                                 FocusIndicator.panel.Refresh(); //necessary so that red sprite doesnt linger
  25.                         FocusIndicator.panel = focusZone.panel;
  26.                         //do this so that setter in UIWidget's depth property updates panel
  27.                         FocusIndicator.depth = FocusIndicator.depth == Int32.MaxValue ? Int32.MaxValue - 1 : Int32.MaxValue;
  28.                 }
  29.         }
  30.  
  31.         void Update()
  32.         {
  33.                 if(Input.GetKeyDown(KeyCode.Backspace))
  34.                 {
  35.                         if(leftSelected)
  36.                         {
  37.                                 NGUITools.SetActive(parentPanel.gameObject, true);
  38.                                 ChangeFocus(focusRight);
  39.                         }
  40.                         else
  41.                         {
  42.                                 NGUITools.SetActive(parentPanel.gameObject, false);
  43.                                 ChangeFocus(focusLeft);
  44.                         }
  45.                         leftSelected = !leftSelected;
  46.                 }
  47.         }
  48. }
  49.  
Layout

Switching the order of the two lines in the else statement makes the bug go away, but my actual project is complex enough so that changing the order of these two events would require some ugly refactoring.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Weird render bug after changing a sprites panel from a disabled one.
« Reply #1 on: November 15, 2014, 04:29:22 PM »
Whenever you change the sprite's parent, you need to inform it: NGUITools.MarkParentAsChanged. You should also *never* be changing the panel of a widget yourself.

Change the transform's parent, call MarkParentAsChanged. That's it.

gerardo

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Weird render bug after changing a sprites panel from a disabled one.
« Reply #2 on: November 17, 2014, 01:14:05 PM »
Sweet. I didn't know about that function. Maybe I should give NGUITools a scan. Thanks.