Author Topic: Drag and Drop does not Update the Index of the new positions.  (Read 3836 times)

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
I'm trying to make a horizontal scrollView in which the user can re order the items. After which, the ordering is saved so that when the user comes back to the app, they can see the same ordering again.  It seems that the scrollView can reorder things graphically but the ChildList is not reflecting the correct index.

Case:
1. Horizontal ScrollView with 3 items, set to horizontal sorting.
2. When item1 is dragged to item2, graphically it replaces the item2 position.  Even in the Grid's hack code, it seems to try to reparent with the correct index.
3. Problem is that after it does it's reparenting, the index is not updated to the new position.

Link to repos.
https://dl.dropboxusercontent.com/u/48378123/DragDropIndexTest.unitypackage

1. Drag  item1 to item2
2. Click on any of the buttons to see the child list. 

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag and Drop does not Update the Index of the new positions.
« Reply #1 on: May 17, 2014, 06:31:50 PM »
I just looked into this and looks like it's some yet another issue in Unity. I added some Debug.Log statements inside the ResetPosition() function in the grid, and it changes the transforms correctly, then re-adds them in the proper order. Unfortunately if I do a GetChildList() right after it's still in the old order, as if nothing has happened. So short-term solution? Just sort the items after you retrieve the list, and don't trust anything Unity tells you.
  1.         public BetterList<Transform> GetChildList()
  2.         {
  3.                 Transform myTrans = transform;
  4.                 BetterList<Transform> list = new BetterList<Transform>();
  5.  
  6.                 for (int i = 0; i < myTrans.childCount; ++i)
  7.                 {
  8.                         Transform t = myTrans.GetChild(i);
  9.                         if (!hideInactive || (t && NGUITools.GetActive(t.gameObject)))
  10.                                 list.Add(t);
  11.                 }
  12.  
  13.                 // Sort the list using the desired sorting logic
  14.                 if (sorting != Sorting.None)
  15.                 {
  16.                         if (sorting == Sorting.Alphabetic) list.Sort(SortByName);
  17.                         else if (sorting == Sorting.Horizontal) list.Sort(SortHorizontal);
  18.                         else if (sorting == Sorting.Vertical) list.Sort(SortVertical);
  19.                         else if (onCustomSort != null) list.Sort(onCustomSort);
  20.                         else Sort(list);
  21.                 }
  22.                 return list;
  23.         }

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Re: Drag and Drop does not Update the Index of the new positions.
« Reply #2 on: May 17, 2014, 09:43:39 PM »
Wait a minute. If Unity has this problem, then this means that the hack's approach needs to be amended.  As it is at the moment, one cannot use the hack to reliably get the correct order.  This should have been tested before hand. 

In my case, how do you propose to get the exact order of the objects after the user has dragged the objects around.  What sorting order would you use.  What hackery should I employ here.

Edit: If you don't mind, I would also prefer to get the solution to this problem via a unity package.  You've requested for me to send repos of the problems.  This seems to be an effective and quickest way to find the problem.  Likewise, instead of a a million questions of the how the solution should be, please reciprocate with a solution repos.  You can post a dropbox link to the file so anyone needing the solution could also get the answer. 
« Last Edit: May 17, 2014, 10:33:22 PM by wallabie »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag and Drop does not Update the Index of the new positions.
« Reply #3 on: May 18, 2014, 11:22:20 AM »
Replacing the function is enough, but if you email me your OR# and I'll email you the updated file. (support at tasharen.com)

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Re: Drag and Drop does not Update the Index of the new positions.
« Reply #4 on: May 19, 2014, 12:04:57 AM »
Great, just tried this and seems to have fixed the problem.