UE4 如何獲取鼠標點擊時的坐標
使用PlayerController獲取
1,獲取鼠標在當(dāng)前場景中坐標系統(tǒng)的中的position,加入場景地圖的范圍是一千平方米,那么這個position的范圍也是1000米x1000米。
注冊鼠標事件
- FInputActionBinding &action1 = InputComponent->BindAction("SetDestination", IE_Pressed, this, &AHPlayerController::OnSetDestinationPressed);
函數(shù)實現(xiàn)MoveToMouseCursor(),此函數(shù)放在PlayerController::PlayerTick()內(nèi)調(diào)用,重寫下PlayerTick():
- void AHPlayerController::MoveToMouseCursor()
- {
- // Trace to see what is under the mouse cursor
- FHitResult Hit;
- GetHitResultUnderCursor(ECC_Visibility, false, Hit);
- if (Hit.bBlockingHit)
- {
- // We hit something, move there
- SetNewMoveDestination(Hit.ImpactPoint);
- }
- }
2,獲取鼠標再顯示屏內(nèi)的坐標系統(tǒng)的position。假如屏幕分辨率是1280x720,那么這個position的范圍就是(0, 0)到(1280, 720)。PlayerController::GetMousePosition()。
- AHPlayerController* PC = ...
- float LocX = 0;
- float LocY = 0;
- PC->GetMousePosition(LocX, LocY);
3,觸屏設(shè)備上獲取場景內(nèi)點擊的position,其范圍與第1種情況相同。
注冊touch事件
- InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AHPlayerController::MoveToTouchLocation);
函數(shù)實現(xiàn):
- void AHPlayerController::MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location)
- {
- FVector2D ScreenSpaceLocation(Location);
- // Trace to see what is under the touch location
- FHitResult HitResult;
- GetHitResultAtScreenPosition(ScreenSpaceLocation, CurrentClickTraceChannel, true, HitResult);
- if (HitResult.bBlockingHit)
- {
- // We hit something, move there
- SetNewMoveDestination(HitResult.ImpactPoint);
- }
- }
使用Viewport接口獲取
- //坐標值是整數(shù)
- FIntPoint MousePoint;
- GEngine->GameViewport->Viewport->GetMousePos(MousePoint);
- //坐標是標準float
- FVector2D CursorPos;
- GEngine->GameViewport->GetMousePosition(CursorPos);
-
分享到:
全部評論:0條