Hello, I am developing a card game and using IBeginDragHandler, IDragHandler, IEndDragHandler. Everything is ok but there is a time limit which allows player to get a card from the deck. If player drag and drop in that limited time there is no problem. If player holds the card dragging and if the time is over, how can I stop dragging and throw card to deck back? I tried destroying and creating the card but it causes some problems. Some Coroutines do not work if i do it. Thanks
Presumably, right now, you have a script somewhere that says something like "while the player is dragging, move the card to follow the pointer". Edit that script so that it checks whether the time limit has expired, and if it has, then instead of following the pointer, put the card back where it started.
Thanks for suggestion. Yes, there is a command line like you said. I changed the line according the to your suggestion. I added a bool. If the time is over bool changes and card turns parent back but there is still problem. This is an online game, so I have to run app in an emulator and cant get debug logs. For this reason I cant understand where the problem is. I think that There is a conflict between countdown coroutine and IDraghandler-eventData. I think the solution is to clean(null) data in eventData, but i dont know how to do it. I tried this "OnDrag(null);" but no success.
Why don't you build a debug version and at least put out your debug log into a textfield or textfile? Also why isn't it working out of Unity, should do or not?
This is how I do it - made a public method which I can call to cancel a drag: Code (CSharp): { private PointerEventData _lastPointerData; public void OnBeginDrag (PointerEventData eventData) { _lastPointerData = eventData; } public void OnDrag (PointerEventData eventData) { // Set position here } public void OnEndDrag (PointerEventData eventData) { _lastPointerData = null; // Reset position here } public void CancelDrag () { if (_lastPointerData != null) { _lastPointerData.pointerDrag = null; // Reset position here } } }
Have your Card implement ISelectHandler and IDeselectHandler. and then in OnDeselect... Code (CSharp): public void OnDeselect(BaseEventData eventData) { var pointerData = eventData as PointerEventData; if(pointerData != null) { // if I'm being dragged, change the event data to say I'm no longer dragged if(pointerData.pointerDrag == gameObject) { // This should automatically trigger EndDrag for this card, // OnDrop for any hovered slot, etc. pointerData.pointerDrag = null; } } else { //have to manually reset card } } Then the script that is handling the timer can simply call something like Code (CSharp): private void HandleTimeOut() { //if any card was selected deselect that card EventSystem.current.SetSelectedGameObject(null); } this way other scripts can trigger a card to clean up its state without explicitly having to know about that card, and the card itself doesn't explicitly have to know about the rules for WHEN it should stop being dragged.
Thanks all, I tried all suggestions. But no succesful result. JoshuaMcKenzie your idea looks as closest way but doesnt work (or i couldnt do it). 1- I implemented both ISelectHandler and IDeselectHanler then put a text into them for controlling the method works. The texts are not changed in both side. 2- I added this "EventSystem.current.SetSelectedGameObject(gameObject, eventData);" to IBeginDrag then ISelectHandler worked but OnDeselect It still doesnt work. With these methods I can reach BaseEventData, but I think i have to reach PointerEventData which is hold by EventSystem. Sorry for my confusion and my English.