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ではできません.