Author Topic: UIDragDropItem "Clone On Drag" broken in 3.7.1  (Read 2193 times)

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
UIDragDropItem "Clone On Drag" broken in 3.7.1
« on: August 30, 2014, 07:08:32 PM »
For my project, I'm extending UIDragDropItem so that I can get some custom behavior on Start/Move/Release.

With 3.5.8, all worked perfectly. After upgrading to 3.7.1, the Start method is triggered but never Move/Release (confirmed with debug lines in the method).

After some debugging of NGUI code I found that the bug only occurs when "Clone On Drag" is selected. My guess is that there's something incorrect in UIDragDropItem.StartDragging()

I think I'll be able to figure this bug out on my own (and I'll report back here). Any tips are appreciated though.
« Last Edit: August 30, 2014, 10:12:17 PM by Wisteso »

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
Re: UIDragDropItem "Clone On Drag" broken in 3.7.1
« Reply #1 on: August 30, 2014, 10:10:21 PM »
I've found the solution. It does indeed appear to be an NGUI bug...

In UIDragDropItem.StartDragging() the following code...

  1.         protected virtual void StartDragging ()
  2.         {
  3.                 if (!mDragging)
  4.                 {
  5.                         mDragging = true;
  6.  
  7.                         if (cloneOnDrag)
  8.                         {
  9.                                 GameObject clone = NGUITools.AddChild(transform.parent.gameObject, gameObject);
  10.                                 clone.transform.localPosition = transform.localPosition;
  11.                                 clone.transform.localRotation = transform.localRotation;
  12.                                 clone.transform.localScale = transform.localScale;
  13.  
  14.                                 UIButtonColor bc = clone.GetComponent<UIButtonColor>();
  15.                                 if (bc != null) bc.defaultColor = GetComponent<UIButtonColor>().defaultColor;
  16.  
  17.                                 UICamera.currentTouch.dragged = clone;
  18.  
  19.                                 UIDragDropItem item = clone.GetComponent<UIDragDropItem>();
  20.                                 item.Start();
  21.                                 item.OnDragDropStart();
  22.                         }
  23.                         else OnDragDropStart();
  24.                 }
  25.         }
  26.  

...should be...

  1.         protected virtual void StartDragging ()
  2.         {
  3.                 if (!mDragging)
  4.                 {
  5.             if (cloneOnDrag)
  6.             {
  7.                 GameObject clone = NGUITools.AddChild(transform.parent.gameObject, gameObject);
  8.                 clone.transform.localPosition = transform.localPosition;
  9.                 clone.transform.localRotation = transform.localRotation;
  10.                 clone.transform.localScale = transform.localScale;
  11.  
  12.                 UIButtonColor bc = clone.GetComponent<UIButtonColor>();
  13.                 if (bc != null) bc.defaultColor = GetComponent<UIButtonColor>().defaultColor;
  14.  
  15.                 UICamera.currentTouch.dragged = clone;
  16.  
  17.                 UIDragDropItem item = clone.GetComponent<UIDragDropItem>();
  18.                 item.Start();
  19.                 item.mDragging = true;
  20.                 item.OnDragDropStart();
  21.             }
  22.             else
  23.             {
  24.                 mDragging = true;
  25.                 OnDragDropStart();
  26.             }
  27.                 }
  28.         }
  29.  

...because without mDragging set to true on the clone, OnDrag and StopDragging will both return early when they should not. You also dont want to set it to true on the original, since the clone is the one that is going to be receiving future input events.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDragDropItem "Clone On Drag" broken in 3.7.1
« Reply #2 on: August 31, 2014, 03:10:43 PM »
Wasn't there a thread on this just a few days ago? This change is already in the Pro repository. Thanks though.

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
Re: UIDragDropItem "Clone On Drag" broken in 3.7.1
« Reply #3 on: August 31, 2014, 05:49:32 PM »
Found it. The thread http://www.tasharen.com/forum/index.php?topic=10946.0 is the thread you're talking about. It's basically the same fix, except that it could be a bit more efficient with setting mDragging.