midas+son의 크리에이티브(creative) 이야기

오랜만에 유니티 글이네요.


오브젝트 비교하는 구문을 예로 들자면

if (player.gameobject == other.gameobject)
{

//코드...

}

위와 같이 == 로 비교 하는게 편했고 잘 사용해 왔다.


근데 이 간편한 구문보다 약 2배 빠른 비교 방법이 있다는걸 이제 알았다.

원래는 C#의 문법인데 아래 MSDN 참조해보자.

https://msdn.microsoft.com/ko-kr/library/system.object.referenceequals(v=vs.110).aspx


//이건 C# - if(Object.ReferenceEquals(player.gameobject, other.gameobject))

//유니티에선 아래

if(System.Object.ReferenceEquals(player.gameobject, other.gameobject))

{

//코드...

}

주의 할 점은 Reference에 대한 비교이다 보니 

값이 같아도 Reference위치가 다르거나 Value Type이면 False가 떨어진다는 점이다.

null 비교에도 좋다.//System.Object.ReferenceEquals(gameobject, null)

/*

UPlayerInput::AddEngineDefinedAxisMapping은 더이상 바꿀 수 없는 단일 엔진 키맵핑 설정이다. 

캐릭터 하나나 컨트롤 값을 단일로만 사용할 경우 사용한다.

*/

//UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("NpcMoveForward", EKeys::W, 1.f));

//UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("NpcMoveForward", EKeys::S, -1.f));

//UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("NpcMoveRight", EKeys::D, 1.f));

//UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("NpcMoveRight", EKeys::A, -1.f));

//UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("NpcLookUp", EKeys::E, 1.f));

//UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("NpcLookUp", EKeys::Q, -1.f));

//UPlayerInput::AddEngineDefinedActionMapping(FInputActionKeyMapping("NpcRightMouse", EKeys::Zero));

//UPlayerInput::AddEngineDefinedActionMapping(FInputActionKeyMapping("NpcDelete", EKeys::Delete));


/*

컨트롤러에 있는 PlayerInput에 있는 키맵핑수정 가능한 설정이다.

캐릭마다 여러 컨트롤 값을 사용할 경우 유용하다.

*/

UPlayerInput* PlayerInput = GetWorld()->GetFirstPlayerController()->PlayerInput;

PlayerInput->AddAxisMapping(FInputAxisKeyMapping("NpcMoveForward", EKeys::W, 1.f));

PlayerInput->AddAxisMapping(FInputAxisKeyMapping("NpcMoveForward", EKeys::S, -1.f));

PlayerInput->AddAxisMapping(FInputAxisKeyMapping("NpcMoveRight", EKeys::D, 1.f));

PlayerInput->AddAxisMapping(FInputAxisKeyMapping("NpcMoveRight", EKeys::A, -1.f));

PlayerInput->AddAxisMapping(FInputAxisKeyMapping("NpcLookUp", EKeys::E, 1.f));

PlayerInput->AddAxisMapping(FInputAxisKeyMapping("NpcLookUp", EKeys::Q, -1.f));

PlayerInput->AddActionMapping(FInputActionKeyMapping("NpcUnpossess", EKeys::Zero));

PlayerInput->AddActionMapping(FInputActionKeyMapping("NpcDelete", EKeys::Delete));


//인풋컴포넌트에 함수 바인딩

InputComponent->BindAxis("NpcMoveForward", this, &ANpcCharacter::MoveForward);

InputComponent->BindAxis("NpcMoveRight", this, &ANpcCharacter::MoveRight);

InputComponent->BindAxis("NpcLookUp", this, &APawn::AddControllerPitchInput);

InputComponent->BindAction("NpcUnpossess", IE_Pressed, this, &ANpcCharacter::NpcUnpossess);

InputComponent->BindAction("NpcDelete", IE_Pressed, this, &ANpcCharacter::NpcDelete);

프로젝트 설정에서 하나하나 지정하는 것 말고 

C++에서 처리 하는 방식을 찾아 정리하였다.


키맵핑을 삭제 하는 방법은 다음 글로 포스팅 하겠다.

http://midason.tistory.com/420

GetWorld()->GetFirstPlayerController()->Possess(this); //현재 pawn으로 빙의. this는 pawn을 상속받은 클래스

Possess는 소유하다, 지니다는 뜻으로 Controller를 지니게되는 Pawn을 정해준다.

APawn*을 인자로 받는다.(APawn을 상속받은 하위 클래스 포함)

인자로 들어온 pawn으로 Controller와 카메라 시점을 이동할 수 있다.


이것에 대해 아래와 같은 연관되게 되는 함수가 있다.

//현재 Pawn에 대해

//SetupPlayerInputComponent(GetWorld()->GetFirstPlayerController()->InputComponent); //키맵핑 하는 함수

//Possess()함수를 실행하면서 SetupPlayerInputComponent 함수도 같이 실행됨. 그래서 따로 함수 호출을 안해도 된다.


//과거 Pawn에 대해

//GetWorld()->GetFirstPlayerController()->UnPossess();

//이전의 pawn에 있던 Controller가  nullptr이 된다.

위의 함수들은 Possess할 때 자동으로 불리우며

과거와의 연을 끊고 새로운 연을 잇는다고 생각하면 편하다.


정리

Possess()함수를 호출하면 

이전에 Controller가 빙의해있던 pawn은 UnPossess()가 실행 되면서 Controller가 nullptr이 된다.

현재 이동할 pawn이 Controller를 가지게 되고

자동으로 SetupPlayerInputComponent(UInputComponent*)함수를 호출한다.


SetupPlayerInputComponent()함수에서 키맵핑 하는 법은 다음 글에 이어서 하겠다.