코드 및 공부/입력 관리

커스텀 컨트롤 바인딩 (InputSystem을 이용한 Rebinding system)

ekrxjvpvj0110 2025. 2. 25. 23:52

유니티 버전 - 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 키를 이용하여 수행하도록 변경하였고 정상적으로 작동합니다

 

 

 

 

 

 

인풋 시스템 리바인딩 관련하여 참고 가능한 자료입니다

더보기

 

 

 

다음번에는 변경된 키세팅을 저장하고 불러오는 기능을 구현해보겠습니다, 이상입니다