포스트

향상된 입력 시스템 - 마우스 클릭 구현하기

이 글은 제 개인적인 공부를 위해 작성한 글입니다.
틀린 내용이 있을 수 있고, 피드백은 환영합니다.


개요


향상된 입력 시스템으로 왼쪽 마우스 클릭 시 이벤트를 C++ 코드로 구현해보았다.

간단하게 필요한 과정을 정리하자면,

  1. 헤더 파일 작성
  2. 생성자
  3. SetupPlayerInputComponent()
  4. BeginPlay()
  5. 입력 액션과 매핑 컨텍스트를 받을 프로퍼티
  6. 마우스 클릭 시 호출될 함수
  7. 소스 파일 작성
  8. 생성자에서 입력 액션과 매핑 컨텍스트를 로딩
  9. BeginPlay()에서 매핑 컨텍스트를 추가
  10. SetupPlayerInputComponent()에서 입력 액션을 바인딩
  11. 마우스 클릭 시 호출될 함수 작성
  12. 에셋(입력 액션, 매핑 컨텍스트) 생성


1. 필요한 헤더 구현하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once
#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "UWCharacterPlayer.generated.h"

UCLASS()
class YOURPROJECT_API ACharacterPlayer : public ACharacter
{
	GENERATED_BODY()

public:	
	ACharacterPlayer();
	
protected:
	virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, Meta = (AllowPrivateAccess = "true"))
	TObjectPtr<class UInputMappingContext> DefaultMappingContext;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, Meta = (AllowPrivateAccess = "true"))
	TObjectPtr<class UInputAction> LeftClickAction;

	void OnLeftClick(const FInputActionValue& Value);
};


2. 소스 파일 구현하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Fill out your copyright notice in the Description page of Project Settings.

#include "ACharacterPlayer.h"
#include "InputMappingContext.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"

ACharacterPlayer::ACharacterPlayer()
{
	static ConstructorHelpers::FObjectFinder<UInputMappingContext> InputMappingContextRef(TEXT("/Script/EnhancedInput.InputMappingContext'/Game/Input/IMC_Default.IMC_Default'"));
	if (nullptr != InputMappingContextRef.Object)
	{
		DefaultMappingContext = InputMappingContextRef.Object;
	}

	static ConstructorHelpers::FObjectFinder<UInputAction> InputActionLeftClickRef(TEXT("/Script/EnhancedInput.InputAction'/Game/Input/Actions/IA_LeftClick.IA_LeftClick'"));
	if (nullptr != InputActionLeftClickRef.Object)
	{
		LeftClickAction = InputActionLeftClickRef.Object;
	}
}

void ACharacterPlayer::BeginPlay()
{
	Super::BeginPlay();

	APlayerController* PlayerController = CastChecked<APlayerController>(GetController());
	if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
	{
		Subsystem->AddMappingContext(DefaultMappingContext, 0);
	}
}

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

	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);
	EnhancedInputComponent->BindAction(LeftClickAction, ETriggerEvent::Triggered, this, &AUWCharacterPlayer::OnLeftClick);
}

void ACharacterPlayer::OnLeftClick(const FInputActionValue& Value)
{
	UE_LOG(LogTemp, Log, TEXT("Mouse Left Button Clicked!"));
}


3. 입력 액션 및 매핑 컨텍스트 생성하기


  1. 입력 액션 생성
  2. 컨텐츠 브라우저에서 Input → Input Action 생성
  3. 이름을 “IA_LeftClick”으로 설정하고 적절한 경로에 배치
  4. 속성에서 입력 타입을 Boolean으로 설정
  5. 입력 매핑 컨텍스트 생성
  6. 컨텐츠 브라우저에서 Input → Input Mapping Context 생성
  7. 이름을 “IMC_Default”로 설정
  8. 생성된 매핑 컨텍스트를 열고 +를 클릭하여 새 매핑 추가
  9. 앞서 만든 “IA_LeftClick” 액션 선택
  10. Key를 Left Mouse Button으로 설정
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.