Author Topic: What is good code to move between NGUI UIscrollviews?  (Read 426 times)

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
What is good code to move between NGUI UIscrollviews?
« on: May 14, 2017, 03:56:12 AM »
So my game now has 3 decks, Hand, Discard, Draw decks. And cards in game frequently move between these 3 decks.

What is good code to move one scrollview's contents to another scrollview?

I used this code, but I don't know this is proper and whether having redundant line or not.

And sometimes move many cards in once, some cards are missing and card's transform appear instantly on center of camera and then moved to another, this is also annoying thing.

-------------
  1. public void MoveObjToNGUITransform(GameObject go,  UIGrid grid)    
  2.     {
  3.         if (go == null)
  4.             return;
  5.         go.transform.parent = grid.transform;
  6.         //go.transform.position = CardManager.Instance.Tf_UI_Root.transform.position;
  7.         go.transform.localScale = new Vector3(1f, 1f, 1f);
  8.         go.transform.rotation = Quaternion.identity;
  9.         grid.Reposition();
  10.         grid.repositionNow = true;
  11.         grid.transform.parent.GetComponent<UIScrollView>().ResetPosition();
  12.         NGUITools.MarkParentAsChanged(go);
  13.         SetCard.Instance.ShowDeckNum();
  14.     }
  15.  

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: What is good code to move between NGUI UIscrollviews?
« Reply #1 on: May 14, 2017, 04:06:38 AM »
"go" is small card's object that actually should move between scrollviews.

and "grid" parameter is one of 3 deck's UIGrid.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 316
  • -Receive: 1149
  • Posts: 22,027
  • Toronto, Canada
    • View Profile
Re: What is good code to move between NGUI UIscrollviews?
« Reply #2 on: May 19, 2017, 04:49:18 AM »
If you are calling grid.Reposition(), there is no need to set 'repositionNow' as well. The marking of parent as changed needs to happen as soon as you change the transform's parent, not later. Also, you can just have a look at UIDragDropItem's OnDragDropRelease function, it handles everything related to changing the dragged object's parent there -- but in your case I am guessing you need far less. In short, aside from the things I mentioned above, what you're doing is fine.

leegod

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: What is good code to move between NGUI UIscrollviews?
« Reply #3 on: May 19, 2017, 08:53:53 AM »
If you are calling grid.Reposition(), there is no need to set 'repositionNow' as well. The marking of parent as changed needs to happen as soon as you change the transform's parent, not later. Also, you can just have a look at UIDragDropItem's OnDragDropRelease function, it handles everything related to changing the dragged object's parent there -- but in your case I am guessing you need far less. In short, aside from the things I mentioned above, what you're doing is fine.
  1. public void MoveObjToNGUITransform(GameObject go,  UIGrid grid)    
  2.     {
  3.         if (go == null)
  4.             return;
  5.         go.transform.parent = grid.transform;
  6.         NGUITools.MarkParentAsChanged(go);
  7.         go.transform.localScale = new Vector3(1f, 1f, 1f);
  8.         go.transform.rotation = Quaternion.identity;
  9.         grid.Reposition();
  10.         grid.transform.parent.GetComponent<UIScrollView>().ResetPosition();        
  11.         SetCard.Instance.ShowDeckNum();
  12.     }
  13.  

Thx, So I revised like above, is this fine?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 316
  • -Receive: 1149
  • Posts: 22,027
  • Toronto, Canada
    • View Profile
Re: What is good code to move between NGUI UIscrollviews?
« Reply #4 on: May 25, 2017, 06:17:44 AM »
I'd say change "rotation" to "localRotation" to match your "localScale" but other than that looks fine.