https://forum.unity.com/threads/detect-most-recent-input-device-type.753206/#post-5360982
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;
}
}
}
'🌍 Unity > 유니티 엔진 동작 관련' 카테고리의 다른 글
unity 언제나 특정 씬에서 시작하게 만들기 (0) | 2022.11.21 |
---|---|
Unity attribute [field: SerializeField] (0) | 2022.11.20 |
유니티 새로운 인풋 시스템 Unity New InputSystem (1) | 2022.10.02 |
ISerializationCallbackReceiver 시리얼라이즈 콜백 활용하기 (1) | 2022.05.12 |
Unity GameObject의 OnDestroy의 호출은 보장되지않는다. (1) | 2022.01.14 |