cesium for Unreal实现鼠标左键拖动?
首先,需要在Unreal中创建一个Actor或者一个Component,并将其绑定到Cesium3DTileset。
然后,在Actor或者Component中重写以下两个函数:
void OnMouseButtonDown(FKey ButtonPressed);
void OnMouseButtonUp(FKey ButtonReleased);
这两个函数分别处理鼠标左键按下和释放的事件。
在OnMouseButtonDown函数中,可以使用以下代码来获取当前鼠标的位置:
FVector2D mousePosition = FVector2D();
if (UGameViewportClient* gameViewport = GEngine->GameViewport)
{
gameViewport->GetMousePosition(mousePosition);
}
接着,可以使用以下代码来获取鼠标在屏幕上的位置对应的世界坐标:
FVector worldPosition = FVector();
FVector worldDirection = FVector();
if (UGameplayStatics::DeprojectScreenToWorld(GetWorld(), mousePosition, worldPosition, worldDirection))
{
// do something with worldPosition and worldDirection
}
在OnMouseButtonUp函数中,可以根据鼠标按下和释放时的位置差来实现鼠标左键拖动效果:
if (isDragging)
{
FVector2D currentMousePosition = FVector2D();
if (UGameViewportClient* gameViewport = GEngine->GameViewport)
{
gameViewport->GetMousePosition(currentMousePosition);
}
FVector2D dragDelta = currentMousePosition - previousMousePosition;
// do something with dragDelta
previousMousePosition = currentMousePosition;
}
需要注意的是,为了使OnMouseButtonDown和OnMouseButtonUp函数生效,需要在Actor或者Component的构造函数中调用以下代码:
bEnableMouseOverEvents = true;
bEnableClickEvents = true;
bEnableTouchOverEvents = true;
bEnableTouchEvents = true;
``
原文地址: https://www.cveoy.top/t/topic/fG2l 著作权归作者所有。请勿转载和采集!