Input System 마우스키보드 게임패드 동시지원

 

https://forum.unity.com/threads/detect-most-recent-input-device-type.753206/#post-5360982

 

Detect most recent input device/type?

I'm trying to integrate/switchto the new input system and I'm stuck at the following issue. My game supports gamepad, as well as mouse and keyboard...

forum.unity.com

 

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Users;
 
// add this into built-in button you can create from menu: UI/Button
public class InputDeviceChangeHandler : MonoBehaviour {
   // refs to Button's components
    private Image buttonImage;
 
   // refs to your sprites
    public Sprite gamepadImage;
    public Sprite keyboardImage;
 
    void Awake() {
        buttonImage = GetComponent<Image>();
        PlayerInput input = FindObjectOfType<PlayerInput>();
        updateButtonImage(input.currentControlScheme);
    }
 
    void OnEnable() {
        InputUser.onChange += onInputDeviceChange;
    }
 
    void OnDisable() {
        InputUser.onChange -= onInputDeviceChange;
    }
 
    void onInputDeviceChange(InputUser user, InputUserChange change, InputDevice device) {
        if (change == InputUserChange.ControlSchemeChanged) {
            updateButtonImage(user.controlScheme.Value.name);
        }
    }
 
    void updateButtonImage(string schemeName) {
       // assuming you have only 2 schemes: keyboard and gamepad
        if (schemeName.Equals("Gamepad")) {
            buttonImage.sprite = gamepadImage;
        }
        else {
            buttonImage.sprite = keyboardImage;
        }
    }
}

 

 

댓글

Designed by JB FACTORY