Author Topic: What's the proper pattern for moving a widget from one panel to another?  (Read 2822 times)

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
I'm attempting to move an object with several widgets on it to another panel temporarily at runtime. It seems like it should be very straightforward, but I can't get it to work. Here's my best guess at how it's supposed to work:

  1. widget.cachedTransform.parent = newPanel.cachedTransform;
  2. oldPanel.RemoveWidget(widget);
  3. newPanel.AddWidget(widget);
  4.  

Even setting both panels as dirty doesn't work. Anyone know the right way to do this?

EDIT: It does work as expected if you disable the widget(s) before changing the parent, then reenable it afterward. Ditto with the panel's GameObject. I'm not sure this is the intended method, though.
« Last Edit: April 15, 2014, 01:48:43 PM by AtomicBob »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Whenever you change the parent, you need to notify the widget via NGUITools.MarkParentAsChanged(objectThatWasReparented);

Also... never use UIPanel.RemoveWidget/AddWidget. They're not meant to be used by users. Simply changing the parent then calling MarkParentAsChanged is enough.

P.S. Drag & drop example does the re-parenting correctly if you need an example.