using UnrealBuildTool; public class MyTPS : ModuleRules { public MyTPS(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" }); PrivateDependencyModuleNames.AddRange(new string[] { }); } }
BeginPlay : 게임 시작 시 UserWidget을 생성한다.
SetupPlayerInputComponent : 키 입력시 호출되는 함수(BindAction)에서 위젯을 Viewport에서 붙이거나 제거한다.
UPROPERTY(EditAnywhere) TSubclassOf<class UUserWidget> sniperUIFactory; class UUserWidget* sniperUI; void OnActionZoomIn(); void OnActionZoomOut();
void ATPSPlayer::BeginPlay() { Super::BeginPlay(); // ... sniperUI = CreateWidget(GetWorld(), sniperUIFactory); } void ATPSPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); // ... PlayerInputComponent->BindAction(TEXT("Zoom"), IE_Pressed, this, &ATPSPlayer::OnActionZoomIn); PlayerInputComponent->BindAction(TEXT("Zoom"), IE_Released, this, &ATPSPlayer::OnActionZoomOut); } void ATPSPlayer::OnActionZoomIn() { if (sniperUI->IsInViewport() == false) // 안전장치 { sniperUI->AddToViewport(); } cameraComp->SetFieldOfView(30.f); // FOV 변경 } void ATPSPlayer::OnActionZoomOut() { if (sniperUI->IsInViewport() == true) // 안전장치 { sniperUI->RemoveFromParent(); } cameraComp->SetFieldOfView(90.f); // FOV 초기화 }