How to drag an object from a layout group and remove it at the same time
Hello friends,
I'm currently working on the UI of my game. For this, I have a UI canvas which has a vertical layout group. In this layout, you can select from multiple heroes for a mission. The selection should work by dragging a hero from the layout group to a slot to assign it for the mission. I want the entry in the layout group removed while I can keep dragging a portrait of the selected hero to a drop area, where it then is assigned for the mission. So the general flow should be like this:
Click on entry in the layoutgroup
Instantiate portrait image, which is draggable
Delete entry (or set inactive)
If portrait is dragged to slot -> assign
If portrait is not dragged to slot -> destroy image, set entry active again
I currently have each entry clickable, which instantiates the portrait image. I then pass on the OnDrag() from the entry to the image, which keeps it dragable. But when I set the entry to inactive, the image is not dragable anymore without clicking on it again, which is probably because the OnDrag() of the entry can not be called and thus is not passed on to the image.
Is there any way that I click on the entry, instantiate an image representing that entry, set the entry itself to inactive and still be able to move the image without having to click the image again?
Thank you very much!
Answer by Harinezumi · Feb 08, 2018 at 03:31 PM
I think the issue is that Unity handles internally what object you are dragging, so the moment you click on your item, it will be selected as being dragged, and it will be the object dragged until you release it. But when you set it inactive, it doesn't receive the OnDrag() events anymore.
Can you try setting in inactive by changing the Interactable to false (assuming that you use Unity UI and that's not how you were trying so far)?
If this doesn't solve it, a possible workaround is to have 2 buttons on top of each other, the top one being the active button that you drag, and below it an inactive version, which is just an image and can't be dragged.