Hello Guest

Author Topic: How to have a draggable object with 2D toolkit?  (Read 15142 times)

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
How to have a draggable object with 2D toolkit?
« on: December 01, 2013, 08:03:22 pm »
Hi,

How can I have a draggable object that has 2D toolkit sprite on it? Do I have to do anything special or it's regular objects with 2DTK componenet attached to them?

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #1 on: December 01, 2013, 08:37:33 pm »
Look into the draggable button in the tk2dUI example. Thats one way of doing it.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #2 on: December 01, 2013, 09:26:02 pm »
Thanks for the reply. My game requires a lot of draggable objects, can I do it with the button? I don't want toggle and stuff.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #3 on: December 01, 2013, 09:38:02 pm »
If you have a lot of them you should probably make a behaviour to handle it. The tk2dUIDragItem script will be a good place to start. The entire ui system can be tweaked, eg. turn off button highlights, etc and you have a draggable sprite.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #4 on: December 02, 2013, 01:26:19 am »
Thanks but can't get my head around doing that. Do you have any other solutions? What I want is a simple drag and drop of my animated sprites.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #5 on: December 02, 2013, 06:12:33 pm »
The tk2dUIDragItem scripts is about as easy as it gets without coding. All you need is a tk2dUIItem on the sprite, followed by a tk2dUIDragItem on the same sprite. Make sure the sprite has a collider and you should be able to drag it. (The camera in the scene requires a tk2dUICamera script on it)

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #6 on: December 15, 2013, 04:06:25 pm »
1) Thanks but I still cannot get it to work.

Here is what I've done:

Created a quad, removed it's collider and added a Box Collider 2D to it, added Tk 2D UI camera to my main camera, added a UI Manager (from 2k td), added Tk 2D UI Item and Tk 2D UIDrag Item to my quad , in the UI Manager I selected my camera (that has the tk 2d UI camera script attached to it)

What am I missing?

BTW, I was testing with mouse, but I think it should work, right?

Thanks.

EDIT


I found the problem, apparently TK 2D doesn't work with 2D colliders and when I reverted back to Box Collider, it worked. Please do correct me if I'm wrong.

Thanks

2) is this a good idea to have this in my game when there are like more than 30 of draggable objects that move accross the screen at a time? I'm asking because it's named "UI" and I wonder if it has any considerable overhead that is not advisable to be treated as a regular draggable object for heavy lifting.

3) When my object gets touched, can I catch an event or message so I can handle some game logic with it? For example in my game these draggable objects are moving constantly and when you touch them, they have to stop and in order to detect that, I have to know when TK 2D detects a touch on them that I can catch as well.

3) Can I get the vector or direction that my obeject was getting dragged? In my game, when player draggs and releases the draggable object, I want my object to move along the direction that it was getting dragged, as like it's getting thrown in 2D.

Thanks again.
« Last Edit: December 15, 2013, 04:32:13 pm by test84 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #7 on: December 15, 2013, 07:48:26 pm »
You're right it only works with 3D colliders. 2D may be supported in the future, but there are no concrete plans to do so just yet.

2. Its perfectly fine - only one ray cast is performed for all colliders. Its much more efficient than some other approaches.
3. You can hook into the necessary events here: http://2dtoolkit.com/docs/2.3/ui/system.html
3. No, but you can attach a behaviour that sums up an average movement vector.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #8 on: December 18, 2013, 10:18:20 pm »
Thanks, that did it.

Would you please help me with the number 3, the sum of movement behavior?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #9 on: December 19, 2013, 12:00:07 am »
Bascially you want to accumulate how much you've moved while you're clicking. Just keep note of the start position, then calculate the delta every frame and create an average with that. When you let go, use the average velocity and dampen it over time to have your sprite drag on for a bit...

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #10 on: January 28, 2014, 03:54:37 pm »
Bascially you want to accumulate how much you've moved while you're clicking. Just keep note of the start position, then calculate the delta every frame and create an average with that. When you let go, use the average velocity and dampen it over time to have your sprite drag on for a bit...

Thanks.

Since I have to write code in order to work with 2D Toolkit, would you please tell me where I should write this code? i.e. which events should I listen to for when player drags and releases dragging the object.

I mean since it has to work with UIdrag, my script should be able to listen to those events, right?

Problem is, if player doesn't do a simple drag and release and moves the item a bit around, when she releases it, it would still launch based on first and last position and do not take the middle movements into account.

I checked the UIDrag code and I think you think of them as buttons, am I right? So I think I should copy and paste your UIDrag and do the accumulations and whatnot in the ButtonRelease event. I did that but the simple drag and drop doesn't work anymore!

My script is copy paste of your tk2dUIDragItem script and just renamed the class and file name so it wouldn't clash but in the editor I can see two new public fields UI Item and UI Manager that are both set to Null but your tk2dUIDragItem doesn't show these fields. Why is that and also why they don't get automatically get value as your tk2dUIDragItem does.

Another strange difference is that your script components in the editor do not expose the word "Script" in their name but whatever script I put on an GameObject, does! Why is that?
« Last Edit: January 28, 2014, 04:18:57 pm by test84 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #11 on: January 29, 2014, 12:00:17 pm »
you need to copy the editor class as well - tk2dUIDragItemEditor.cs, duplicate and change appropriately for your class.

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #12 on: February 12, 2014, 04:34:06 pm »
you need to copy the editor class as well - tk2dUIDragItemEditor.cs, duplicate and change appropriately for your class.

I still cannot achieve this. Would you please help me further on this?

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #13 on: February 12, 2014, 06:22:13 pm »
What have you tried? You need to duplicate that class e.g. tk2dYourDragItemEditor, then change the references to tk2dUIDragItem in the c# source. It should just work once you've done this. It must be in an editor folder

test84

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: How to have a draggable object with 2D toolkit?
« Reply #14 on: February 15, 2014, 04:28:01 pm »
OK, finally got it working with the duplocation of those 2 files.

1- Can I move my copies of tk2dUIDragItem and tk2dUIDragItemEditor somewhere out of the tk2d directory so it'll be prone to updates of tk2d?

2- I'm trying to get the average on drag and release but can't get it to work. I've tried to get the average of current position and last position in UpdateBtnPosition and pass it to rigidbody.addForce in ButtonRelease, but it doesn't work.