Unreal Engine 4:如何处理触屏事件

 

目录

一、处理屏幕点击事件

二、处理屏幕双击事件

三、处理屏幕长按事件

四、获取touch在World中的点击位置


对于触控屏幕,如何处理触屏事件,下面给出了一些常用的C++实现:

一、处理屏幕点击事件

void AModel::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
  Super::SetupPlayerInputComponent(PlayerInputComponent);

  PlayerInputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AModel::DoTouchPressed);
  PlayerInputComponent->BindTouch(EInputEvent::IE_Released, this, &AModel::DoTouchReleased);
};
void AModel::DoTouchPressed(ETouchIndex::Type type, FVector v)
{
};
void AModel::DoTouchReleased(ETouchIndex::Type type, FVector v)
{
};

二、处理屏幕双击事件

void AModel::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
  Super::SetupPlayerInputComponent(PlayerInputComponent);

  PlayerInputComponent->BindTouch(EInputEvent::IE_DoubleClick, this, &AModel::DoTouchDoubleClick);
};
void AModel::DoTouchDoubleClick(ETouchIndex::Type type, FVector v)
{
};

三、处理屏幕长按事件

// 需要长按多长时间。单位(毫秒)
int32 AModel::pressTimeSpan = 400;
int64 AModel::TouchPressedTime = 0;

void AModel::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
  Super::SetupPlayerInputComponent(PlayerInputComponent);

  PlayerInputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AModel::DoTouchPressed);
  PlayerInputComponent->BindTouch(EInputEvent::IE_Released, this, &AModel::DoTouchReleased);
  PlayerInputComponent->BindTouch(EInputEvent::IE_Repeat, this, &AModel::DoTouchRepeat);
};
void AModel::DoTouchPressed(ETouchIndex::Type type, FVector v)
{
  TouchPressedTime = FDateTime::Now().GetTicks();
};
void AModel::DoTouchReleased(ETouchIndex::Type type, FVector v)
{
  TouchPressedTime = 0;
};
void AModel::DoTouchRepeat(ETouchIndex::Type type, FVector v)
{
  if (TouchPressedTime == 0) {
    return;
  }
  int64 TouchReleasedTime = FDateTime::Now().GetTicks();

  int64 timeUsed = (TouchReleasedTime - TouchPressedTime) / 10000;
  if (timeUsed >= pressTimeSpan) {
    // do something you like
  }
  TouchPressedTime = 0;
};

四、获取touch在World中的点击位置

void AAndroidProjectCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
	// handle touch devices
	PlayerInputComponent->BindTouch(IE_Pressed, this, &AAndroidProjectCharacter::TouchStarted);
	PlayerInputComponent->BindTouch(IE_Released, this, &AAndroidProjectCharacter::TouchStopped);
};


void AAndroidProjectCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
{
	APlayerController* PlayerController = Cast(GetController());

	FHitResult HitResult;
	PlayerController->GetHitResultUnderCursorByChannel(ETraceTypeQuery::TraceTypeQuery1, true, HitResult);

	// 在Windows和Android下都可取到正常的PositionLocation
	FVector PositionLocation = HitResult.Location;
	GEngine->AddOnScreenDebugMessage(-1, 60.f, FColor::Green, FString::Printf(TEXT("TouchStarted: PositionLocation=%s"), *PositionLocation.ToString()));
};

void AAndroidProjectCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
{
	APlayerController* PlayerController = Cast(GetController());

	FHitResult HitResult;
	PlayerController->GetHitResultUnderCursorByChannel(ETraceTypeQuery::TraceTypeQuery1, true, HitResult);

	// 在Windows下都可取到正常的PositionLocation,但在Android下取不到正常的PositionLocation
	FVector PositionLocation = HitResult.Location;
	GEngine->AddOnScreenDebugMessage(-1, 60.f, FColor::Green, FString::Printf(TEXT("TouchStopped: PositionLocation=%s"), *PositionLocation.ToString()));
};

需要注意IE_Released时,在Windows可以正常取到touch在World中的点击位置,而在Android上却不行。

你可能感兴趣的:(C++,Unreal,Engine,4)