[유니티] Input System 바인딩된 키 표시

jh Seo·2025년 1월 8일
1

유니티

목록 보기
57/62

바인딩된 키의 string값

InputManager.PlayerInput.actions["Move"].bindings[i].ToDisplayString() 

Move라는 액션에 바인딩된 키의 string값을 읽어오려면 위와 같이 작성하면 된다.
i에 몇 번째로 binding된 키인지 넣어주면 된다.

여러개 바인딩된 키


이런 식으로 Move 내부에 Keyboard_Arrows라는 Composite Type과 그 안에 4개의 키 바인딩이 존재한다고 하자.
Up과 Down은 키 바인딩을 안한 상황이고 이 상황에서 Left 와 Right에 바인딩된 string값만 가져오려고 한다.

여기서 단순하게 그냥 Move에 해당하는 action 가져오면 bindings에 총 5개가 들어있다.
0번째 index는 keyboard arrows
1번째 index는 up : no bindings
2번째 index는 Down: no bindings
3번째 index는 left : Left Arrow
4번째 index는 right : right arrow

따라서 3번 4번 인덱스를 호출하면 되지만 그렇게 구현하면 아무래도 다른 액션에서는 사용을 못하니까

 for(int i=0;i< InputManager.PlayerInput.actions["Move"].bindings.Count;i++)
{
	if (InputManager.PlayerInput.actions["Move"].bindings[i].ToDisplayString() == "") continue;
   //TODO: 일단 키보드 마우스만 지원
   tmp += InputManager.PlayerInput.actions["Move"].bindings[i].ToDisplayString();
   tmp += " / ";
}

일단 이런식으로 바인딩 되었는 지 string값을 체크하는 방식으로 구현했다.

control scheme에 따른 키 뽑아오기


이런 식으로 Attack 액션에 Gamepad와 Keyboard 두 개의 키를 같이 넣어줬다.
여기서 bindings를 호출하면 S와 Button West가 같이 호출된다.

이 상황에서 Gamepad에 있는 키를 거르려면 bindings의 path를 조사하면 된다.

string tmp = "";
InputAction curAction = InputManager.PlayerInput.actions[btnToPressStr];
 for (int i = 0; i < curAction.bindings.Count; i++)
 {
     if (curAction.bindings[i].ToDisplayString() == "") continue;
     //TODO: 일단 키보드 마우스만 지원
     if (curAction.bindings[i].path.Contains("<Gamepad>")) continue;
     tmp += curAction.bindings[i].ToDisplayString();
     tmp += " / ";
 }
 if (tmp != null) tmp = tmp.Substring(0, tmp.Length - 3);

위처럼 <Gamepad>가 path에 포함되어 있는지 체크하고 continue로 무시한다.

Gamepad에 대한 처리는 후순위 작업목록이라 일단은 action에 키 바인딩만 해놓고
무시 중이다.

profile
코딩 창고!

1개의 댓글

comment-user-thumbnail
2025년 1월 8일

안녕하세요! 개발자 준비하시는 분이나 현업에 종사하고 계신 분들만 할 수 있는 시급 25달러~51달러 LLM 평가 부업 공유합니다~ 제 블로그에 자세하게 써놓았으니 관심있으시면 한 번 읽어봐주세요 :)

답글 달기

관련 채용 정보