在视觉元素具有拖放行为的操作期间发送拖动事件。该事件只能在编辑器中使用。
要实现拖放功能,需要确保视觉元素会注册特定事件的回调。
支持拖动操作的视觉元素分为两个类型:
您可以选择一个可拖动视觉元素,将其拖动到一个可放置视觉元素,然后释放该元素以将其放置。
所有拖放事件的基类都是 DragAndDropEventBase。
事件 | 描述 | 涓滴 | 冒泡 | 可取消 |
---|---|---|---|---|
DragExitedEvent | 在拖放过程结束时发送。 | ✔ | ✔ | |
DragUpdatedEvent | 在拖动的元素移到放置目标上方时发送。 | ✔ | ✔ | ✔ |
DragPerformEvent | 在拖动的元素放置到目标上方时发送。 | ✔ | ✔ | ✔ |
DragEnterEvent | 在拖动的元素进入新的 VisualElement 时发送。 |
✔ | ||
DragLeaveEvent | 在拖动的元素离开了当前放置目标时发送。 | ✔ |
要使视觉元素可拖动,必须注册以下三种事件类型的回调:
使用以下步骤进行拖动操作:
DragAndDrop
。DragAndDrop.StartDrag()
。DragPerformEvent
或 DragExitedEvent
时应该删除此反馈。当用户在视觉元素之上拖动任何可拖动的对象然后松开鼠标指针时,将发送 DragExitedEvent
。当放置区域视觉元素收到 DragExitedEvent
时,它需要删除所有拖放操作反馈。
当用户移动可拖动的对象时,如果指针移到视觉元素之上,则会发送 DragUpdatedEvent
。
当放置区域视觉元素收到 DragUpdatedEvent
时,它需要更新放置反馈。例如,可以通过将拖动对象的“虚影”移到鼠标指针下来达到此目的。
放置区域视觉元素还应该检查 DragAndDrop
属性,并设置 DragAndDrop.visualMode
来指示放置操作的效果。例如,放置操作可以创建新对象、移动现有对象或拒绝放置操作。
当用户拖动任何可拖动的对象并在视觉元素之上松开鼠标指针时,将发送 DragPerformEvent
。仅在视觉元素将 DragAndDrop.visualMode
设置为 DragAndDropVisualMode.None
或 DragAndDropVisualMode.Rejected
以外的选项来指示它可以接受拖动对象时,才会发生此情况。
当放置区域视觉元素收到 DragPerformEvent
时,它需要对 DragAndDrop.objectReferences
、DragAndDrop.paths
或 DragAndDrop.GetGenericData()
中存储的拖动对象执行操作。
例如,它可能会在用户放置对象的位置添加新的视觉元素。
在拖动操作中,当指针进入视觉元素,则会发送 DragEnterEvent
。
当放置区域视觉元素收到 DragEnterEvent
时,它需要提供反馈,让用户知道它或其子项之一是潜在放置操作的目标。例如,可以将 USS 类添加到目标元素并在鼠标指针下显示拖动对象的“虚影”。
当用户移动可拖动的对象时,如果指针离开视觉元素,则会发送 DragLeaveEvent
。
当放置区域视觉元素收到 DragLeaveEvent
时,它需要停止提供放置反馈。例如,可以在目标元素收到 DragEnterEvent
时删除所添加的 USS 类并不再显示拖动对象的“虚影”。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.