UE4 如何获取鼠标点击时的坐标 - 纳金网
联系我们

给我们留言

联系我们

地址:福建省晋江市青阳街道洪山路国际工业设计园纳金网

邮箱:info@narkii.com

电话:0595-82682267

(周一到周五, 周六周日休息)

当前位置:主页 > 3D教程 > 图文教程

UE4 如何获取鼠标点击时的坐标

来源: 52vr | 责任编辑:传说的落叶 | 发布时间: 2019-06-04 08:29 | 浏览量:

[UE4]如何获取鼠标点击时的坐标

 

使用PlayerController获取

1,获取鼠标在当前场景中坐标系统的中的position,加入场景地图的范围是一千平方米,那么这个position的范围也是1000米x1000米。

注册鼠标事件

 
  1. FInputActionBinding &action1 = InputComponent->BindAction("SetDestination", IE_Pressed, this, &AHPlayerController::OnSetDestinationPressed);  

 函数实现MoveToMouseCursor(),此函数放在PlayerController::PlayerTick()内调用,重写下PlayerTick():

 
  1. void AHPlayerController::MoveToMouseCursor()  
  2. {  
  3.     // Trace to see what is under the mouse cursor  
  4.     FHitResult Hit;  
  5.     GetHitResultUnderCursor(ECC_Visibility, false, Hit);  
  6.   
  7.     if (Hit.bBlockingHit)  
  8.     {  
  9.         // We hit something, move there  
  10.         SetNewMoveDestination(Hit.ImpactPoint);  
  11.     }  
  12. }  

 

2,获取鼠标再显示屏内的坐标系统的position。假如屏幕分辨率是1280x720,那么这个position的范围就是(0, 0)到(1280, 720)。PlayerController::GetMousePosition()。

 
  1. AHPlayerController* PC = ...  
  2.   
  3. float LocX = 0;  
  4. float LocY = 0;  
  5. PC->GetMousePosition(LocX, LocY);  

 

3,触屏设备上获取场景内点击的position,其范围与第1种情况相同。

注册touch事件

 
  1. InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AHPlayerController::MoveToTouchLocation);  

 函数实现:

  1. void AHPlayerController::MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location)  
  2. {  
  3.     FVector2D ScreenSpaceLocation(Location);  
  4.   
  5.     // Trace to see what is under the touch location  
  6.     FHitResult HitResult;  
  7.     GetHitResultAtScreenPosition(ScreenSpaceLocation, CurrentClickTraceChannel, true, HitResult);  
  8.     if (HitResult.bBlockingHit)  
  9.     {  
  10.         // We hit something, move there  
  11.         SetNewMoveDestination(HitResult.ImpactPoint);  
  12.     }  
  13. }  

 

使用Viewport接口获取

 
  1. //坐标值是整数  
  2. FIntPoint MousePoint;  
  3. GEngine->GameViewport->Viewport->GetMousePos(MousePoint);  
  4.   
  5. //坐标是标准float  
  6. FVector2D CursorPos;  
  7. GEngine->GameViewport->GetMousePosition(CursorPos);  


相关文章
网友评论

您需要登录后才可以发帖 登录 | 立即注册

关闭

全部评论:0条

推荐
热门