유니티 버전 - 6000.0.37f1
목차
- 들어가며
- Rebinding system
들어가며
키를 리바인딩할 수 있다면, 다음과 같이 사용자가 직접 원하는 키세팅을 이용하여 동작을 수행하도록 하고, 사용자의 편의성과 접근성을 향상 시킬 수 있습니다
Rebinding system
전체 코드입니다
public class UIRebindAction : MonoBehaviour
{
[SerializeField] private InputActionReference actionReference = null;
[SerializeField] private int bindingIndex;
private InputActionRebindingExtensions.RebindingOperation _rebind;
[Header("UI 요소")]
[SerializeField] private TextMeshProUGUI bindingText;
[SerializeField] private GameObject rebindOverlay; // 리바인딩 중임을 알리기위한 UI
private void OnEnable()
{
bindingText.text = actionReference.action.GetBindingDisplayString(bindingIndex);
}
public void OnStartRebind()
{
rebindOverlay.gameObject.SetActive(true);
actionReference.action.Disable();
_rebind = actionReference.action.PerformInteractiveRebinding()
.WithControlsExcluding("Attack")
.OnMatchWaitForAnother(0.1f)
.WithTargetBinding(bindingIndex)
.OnComplete(operation =>
{
operation.Dispose();
RefreshUI();
});
actionReference.action.Enable();
_rebind.Start();
}
}
actionReference - 인스펙터에서 리바인딩하고자하는 액션을 선택하여 줍니다
bindingIndex - 특정 바인딩의 인덱스를 지정하여 줍니다
Move처럼 하나의 바인딩 안에 여러 개별 바인딩이 있다면 0은 Composite Binding 자체 (AD)를 나타내고, 개별적인 바인딩은 1 부터 시작됩니다
A를 바꾸고싶다면 1, D를 바꾸고싶다면 2를 인덱스로 주어야 합니다
현재 설정된 입력 바인딩을 표시합니다, 만약 Move액션 레퍼런스의 1번째 인덱스라면 원래 설정한 A라는 텍스트가 보입니다
private void OnEnable()
{
bindingText.text = actionReference.action.GetBindingDisplayString(bindingIndex);
}
각 메서드에 대한 설명입니다
actionReference.action.Disable();
// 입력 액션을 일시적으로 비활성화하여 리바인딩 작업 중 기존 입력 이벤트가 발생하지 않도록 합니다
_rebind = actionReference.action.PerformInteractiveRebinding()
// 리바인딩 작업을 시작하기 위해 PerformInteractiveRebinding()을 호출합니다
.OnMatchWaitForAnother(0.1f)
// 입력이 매치되면 추가 입력을 기다리는 시간(0.1초)을 설정합니다 (복합 바인딩의 경우 사용, 단일 설정시 없어도 무방)
.WithTargetBinding(bindingIndex)
// 지정한 bindingIndex에 해당하는 바인딩을 대상으로 리바인딩을 합니다
.OnComplete(operation =>
// 리바인딩 작업이 완료되었을 때 호출될 콜백 함수를 등록합니다
operation.Dispose();
// 작업이 완료되었으므로, 작업에 사용된 리소스를 해제하기 위해 Dispose()를 호출합니다
actionReference.action.Enable();
// 리바인딩 설정이 완료된 후, 입력 액션을 다시 활성화합니다
_rebind.Start();
// 실제로 리바인딩 프로세스를 시작합니다.
결과입니다, 기존 우측 마우스로 수행하던 동작을 q 키를 이용하여 수행하도록 변경하였고 정상적으로 작동합니다
인풋 시스템 리바인딩 관련하여 참고 가능한 자료입니다
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.13/manual/index.html
Input System | Input System | 1.13.1
Input System The Input System allows your users to control your game or app using a device, touch, or gestures. Introduction Unity supports input through two separate systems, one older, and one newer. The older system, which is built-in to the editor, is
docs.unity3d.com
Class InputActionRebindingExtensions.RebindingOperation | Input System | 1.13.1
Class InputActionRebindingExtensions.RebindingOperation An ongoing rebinding operation. Inheritance InputActionRebindingExtensions.RebindingOperation Assembly: Unity.InputSystem.dll Syntax public sealed class InputActionRebindingExtensions.RebindingOperati
docs.unity3d.com
다음번에는 변경된 키세팅을 저장하고 불러오는 기능을 구현해보겠습니다, 이상입니다
'코드 및 공부 > 입력 관리' 카테고리의 다른 글
Rebind한 키세팅 저장/초기화/되돌리기 (0) | 2025.03.18 |
---|---|
input system에서 리바인딩한 키세팅을 저장, 로드, 삭제하기 (0) | 2025.02.28 |
화면 해상도 및 전체화면 여부 조절하기, Screen.SetResolution() (0) | 2025.02.04 |
방향키로 버튼간 이동(탐색) 하기, UI Navigation (0) | 2025.02.03 |
유니티 유용한 단축키 3가지 (0) | 2025.01.14 |