Author Topic: How to center gameobject on mouse when drag and drop  (Read 2740 times)

919710

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
How to center gameobject on mouse when drag and drop
« on: September 19, 2015, 11:44:19 AM »
Hello, Thanks for providing and supporting NGUI! It is a real time saver plugin! I have a tiny question here.

I have a scroll view with a list of weapon [Square Icon] and user are able to drag the weapon1[Square Icon]  to weapon slot [Square Item Slot] ( Using CLONE ), i have no problem with this.

Then i decided to add more stuff to the scrollview/weapon1  , I have now turn it into [Rectangle sized box], and it now has [[Square icon] and some additional info on the right side]

So when user drag from the whole [Rectangle box] i will clone a [Square Icon] prefab , i have no problem with this too.

What I want do now is to center the dragged [Square Icon] into the center of the mouse, because I can start dragging from any point of the [Rectangle box] and the [Square box] appear will some how have an offset from my mouse unless i start dragging from the top left of the [Rectangle box] .

Due to this reason is very hard for me to drop the [Square Icon] into another [Square Item Slot] accurately.

May I know how do i as able to center my [Square icon] position to mouse after its cloned?

1.scrollview
--weapon1[Rectangle box]
--weapon2[Rectangle box]
--weapon3[Rectangle box]

2.user try to drag from rectangle box, cloned a [square box] prefab

3.user drop the [square box] prefab into
-weapon slot[Square Item Slot]


Thanks for your time , much appreciated.
Stev
« Last Edit: September 20, 2015, 02:54:24 AM by 919710 »
Invoice# 302078729

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to center gameobject on mouse when drag and drop
« Reply #1 on: September 20, 2015, 03:03:13 AM »
No need to mention your invoice, and bumping posts on this forum has the opposite effect as I answer them in reverse order (oldest first).

Have a look at the UIDragDropItem.OnDragDropStart function. If you want to add some custom offset / centering, you will want to create a script that inherits UIDragDropItem and overwrites the OnDragDropStart function, adding custom code within that modifies the mTrans.localPosition. Note how OnDragDropMove function simply adds to it?

919710

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: How to center gameobject on mouse when drag and drop
« Reply #2 on: September 20, 2015, 03:32:38 AM »
Hello, Sorry for the inconvenience caused and thanks for the quick reply

I have adapted from your "Drag and Drop Item (Example).cs "  file

1. I have

protected override void OnDragDropRelease (GameObject surface)
{Debug.Log("DROPED ON SURFACE->"+surface);
....
}

and

public override void StartDragging ()
{...to CLONE custom stuff...}


which is working as intended

2. but when i have this in the example cs

public override void OnDragDropStart(){
Debug.Log ("this-> "+draggedItems);
....
}

Its doesn't show debug msg, I attached this example cs on both [rectangle box] and cloned [square icon box] still no luck getting the debug msg.

Then i dig deeper and remembered that for me to use "public override void StartDragging ()" to create custom CLONE item, i actually need to change some of your mTouch and mSomething else from PROTECTED to PUBLIC in UIDragDropItem.cs because

public override void StartDragging (){
UIDragDropItem item = clone.GetComponent<UIDragDropItem>();
item.mTouch
...
}

item.mTouch are giving me error of "UIDragDropItem doesn't contain defination for mTouch and other mSomething else.

Please advise. Thanks for your time.









Invoice# 302078729

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to center gameobject on mouse when drag and drop
« Reply #3 on: September 20, 2015, 04:36:30 AM »
There is no need to access internal components like that. OnDragDropStart gets called on the clone item -- line 200 of UIDragDropItem.cs. However the signature is "protected" not "public".
  1. protected virtual void OnDragDropStart ()

919710

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: How to center gameobject on mouse when drag and drop
« Reply #4 on: September 20, 2015, 09:00:30 AM »
Thanks for the advise

The problem is solved with

public override void StartDragging (){
...
    if (cloneOnDrag)
    {...
       .addchild...
      clone.transform.position = UICamera.lastHit.point;

    ...}

;) thanks!
Invoice# 302078729