? 日韩欧美国产完整版,日本一线二线三卡四卡区别,蝴蝶视频软件下载
聯(lián)系我們

給我們留言

聯(lián)系我們

地址:福建省晉江市青陽街道洪山路國際工業(yè)設(shè)計(jì)園納金網(wǎng)

郵箱:info@narkii.com

電話:0595-82682267

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

當(dāng)前位置:主頁 > 3D教程 > 圖文教程

UE4 如何獲取鼠標(biāo)點(diǎn)擊時(shí)的坐標(biāo)

來源: 52vr | 責(zé)任編輯:傳說的落葉 | 發(fā)布時(shí)間: 2019-06-04 08:29 | 瀏覽量:

[UE4]如何獲取鼠標(biāo)點(diǎn)擊時(shí)的坐標(biāo)

 

使用PlayerController獲取

1,獲取鼠標(biāo)在當(dāng)前場景中坐標(biāo)系統(tǒng)的中的position,加入場景地圖的范圍是一千平方米,那么這個(gè)position的范圍也是1000米x1000米。

注冊鼠標(biāo)事件

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

 函數(shù)實(shí)現(xiàn)MoveToMouseCursor(),此函數(shù)放在PlayerController::PlayerTick()內(nèi)調(diào)用,重寫下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,獲取鼠標(biāo)再顯示屏內(nèi)的坐標(biāo)系統(tǒng)的position。假如屏幕分辨率是1280x720,那么這個(gè)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,觸屏設(shè)備上獲取場景內(nèi)點(diǎn)擊的position,其范圍與第1種情況相同。

注冊touch事件

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

 函數(shù)實(shí)現(xiàn):

  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. //坐標(biāo)值是整數(shù)  
  2. FIntPoint MousePoint;  
  3. GEngine->GameViewport->Viewport->GetMousePos(MousePoint);  
  4.   
  5. //坐標(biāo)是標(biāo)準(zhǔn)float  
  6. FVector2D CursorPos;  
  7. GEngine->GameViewport->GetMousePosition(CursorPos);  


相關(guān)文章
網(wǎng)友評論

您需要登錄后才可以發(fā)帖 登錄 | 立即注冊

關(guān)閉

全部評論:0條

推薦
熱門