要实现拖放功能,必须确保放置区域 VisualElement 和可拖动的 VisualElement 会注册特定事件的回调。本页讨论这些 VisualElement 收到事件时发生的情况。
有关事件的更多信息,请参阅事件系统的相关文档。
作为放置区域的 VisualElement 需要注册以下五种事件类型的回调。
当用户拖动可拖动的对象时,如果指针进入 VisualElement,则会发送 DragEnterEvent
。
当放置区域 VisualElement 收到 DragEnterEvent
时,它需要提供反馈,让用户知道它或其子项之一是潜在放置操作的目标。
例如,可以通过将 USS 类添加到目标元素并在鼠标指针下显示拖动对象的“虚影”来达到此目的。
当用户拖动可拖动的对象时,如果指针退出 VisualElement,则会发送 DragLeaveEvent
。
当放置区域 VisualElement 收到 DragLeaveEvent
时,它需要停止提供放置反馈。
例如,可以通过在目标元素收到 DragEnterEvent
时删除所添加的 USS 类并不再显示拖动对象的“虚影”来达到此目的。
当用户拖动可拖动的对象时,如果指针移到 VisualElement 之上,则会发送 DragUpdatedEvent
。
当放置区域 VisualElement 收到 DragUpdatedEvent
时,它需要更新放置反馈。
例如,可以通过将拖动对象的“虚影”移到鼠标指针下来达到此目的。
放置区域 VisualElement 还应该检查 DragAndDrop
属性,并设置 DragAndDrop.visualMode
来指示放置操作的效果。例如,放置操作可以创建新对象、移动现有对象、拒绝放置操作等等。
当用户拖动任何可拖动的对象并在 VisualElement 之上松开鼠标指针时,将发送 DragPerformEvent
。仅在 VisualElement 将 DragAndDrop.visualMode
设置为 DragAndDropVisualMode.None
或 DragAndDropVisualMode.Rejected
以外的选项来指示它可以接受拖动对象时,才会发生此情况。
当放置区域 VisualElement 收到 DragPerformEvent
时,它需要对 DragAndDrop.objectReferences
、DragAndDrop.paths
或 DragAndDrop.GetGenericData()
中存储的拖动对象执行相应操作。
例如,它可能会在用户放置对象的位置添加新的 VisualElement。
当用户在 VisualElement 之上拖动任何可拖动的对象然后松开鼠标指针时,将发送 DragExitedEvent
。仅在没有 VisualElement 指示它可以接受拖动对象时,才会发生此情况。
当放置区域 VisualElement 收到 DragExitedEvent
时,它需要删除所有拖放反馈。
Note: there is currently a bug in UI Toolkit that prevents DragExitedEvent
from being sent. As a workaround, you can implement the relevant functionality in DragLeaveEvent
, which is sent when you stop a drag and drop operation.
要使 VisualElement 可拖动,必须注册以下三种事件类型的回调。
当可拖动的 VisualElement 收到 MouseDownEvent
时,它需要将状态设置为“准备好拖动”。
当可拖动的 VisualElement 收到 MouseUpEvent
时,它需要重置状态。
当可拖动的 VisualElement 收到 MouseMoveEvent
且它准备好拖动时,它需要:
DragAndDrop
。DragAndDrop.StartDrag()
。DragPerformEvent
或 DragExitedEvent
时应该删除此反馈。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.