오브젝트 빠른 비교 - System.Object.ReferenceEquals
오랜만에 유니티 글이네요.
오브젝트 비교하는 구문을 예로 들자면
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)
'공부 > Unity' 카테고리의 다른 글
유나이트 2017 초급 트레이닝 후기 (0) | 2017.05.15 |
---|---|
유나이트'17 서울 개최(유니티 컨퍼런스) (0) | 2017.03.28 |
유니티에서 배경 이미지 무한 루프 시키기 (0) | 2016.10.07 |
[유니티]드로우콜(Draw call) 최적화 (0) | 2016.09.01 |
포트폴리오 잘하는 법 (0) | 2016.08.25 |
언리얼4 c++프로그래밍 - 키맵핑(바인딩) 설정
/*
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++에서 처리 하는 방식을 찾아 정리하였다.
키맵핑을 삭제 하는 방법은 다음 글로 포스팅 하겠다.
'공부 > Unreal4' 카테고리의 다른 글
실행(플레이) 창 크기, 창 시작점 구하기 - ffmpeg 로 원하는 영역만큼만 녹화해보자 (0) | 2017.04.19 |
---|---|
키맵핑(바인딩) 삭제 - PlayerInput 비우기, InputComponent 비우기 (0) | 2017.04.19 |
언리얼 c++프로그래밍 - Controller의 Possess()함수에 대해 (0) | 2017.04.17 |
FindComponentByClass 의 위험성 (1) | 2017.04.14 |
언리얼 c++에서 스폰된 액터 클래스 명으로 찾기 (0) | 2017.04.05 |
언리얼 c++프로그래밍 - Controller의 Possess()함수에 대해
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()함수에서 키맵핑 하는 법은 다음 글에 이어서 하겠다.
'공부 > Unreal4' 카테고리의 다른 글
키맵핑(바인딩) 삭제 - PlayerInput 비우기, InputComponent 비우기 (0) | 2017.04.19 |
---|---|
언리얼4 c++프로그래밍 - 키맵핑(바인딩) 설정 (0) | 2017.04.17 |
FindComponentByClass 의 위험성 (1) | 2017.04.14 |
언리얼 c++에서 스폰된 액터 클래스 명으로 찾기 (0) | 2017.04.05 |
마이그레이션 중.. #include<Windows.h>문제와 wchar_t to std::string 한글 문제 (0) | 2017.04.05 |