Last active
July 11, 2023 09:55
-
-
Save aprius/c0e7c437ff32b6943aa5f506de58f072 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class ActionCallbackExample : MonoBehaviour | |
{ | |
[SerializeField] private InputAction action; | |
private void OnEnable() | |
{ | |
// Đăng ký callback | |
action.performed += OnPerformed; | |
action?.Enable(); | |
} | |
private void OnDisable() | |
{ | |
// Hủy đăng ký callback | |
action.performed -= OnPerformed; | |
action?.Disable(); | |
} | |
// callback | |
private void OnPerformed(InputAction.CallbackContext context) | |
{ | |
// Đọc giá trị đầu vào của callback | |
var value = context.ReadValue<float>(); | |
Debug.Log($"Action Value : {value}"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class InputActionExample : MonoBehaviour | |
{ | |
[SerializeField] private InputAction action; | |
private void OnEnable() | |
{ | |
// enable InputAction | |
// Lưu ý rằng bạn không thể nhận được input nếu không làm điều này. | |
action?.Enable(); | |
} | |
private void OnDisable() | |
{ | |
// Vô hiệu hóa input action | |
action?.Disable(); | |
} | |
private void Update() | |
{ | |
if (action == null) return; | |
// Đọc giá trị đầu vào của Action | |
var value = action.ReadValue<float>(); | |
Debug.Log($"Value : {value}"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class KeyboardExample : MonoBehaviour | |
{ | |
private void Update() | |
{ | |
// Thông tin hiện tại của keyboard | |
var current = Keyboard.current; | |
// Kiểm tra kết nối của bàn phím | |
if (current == null) | |
{ | |
// Nếu current bằng null tức là không có bàn phím nào được phát hiện. | |
// Chúng ta sẽ return luôn | |
return; | |
} | |
// Lấy ra trạng thái của phím A | |
var aKey = current.aKey; | |
// Nếu phím A chưa được nhấn thì return | |
if (aKey == null) | |
{ | |
return; | |
} | |
// Thời điểm phím A được nhấn | |
if (aKey.wasPressedThisFrame) | |
{ | |
Debug.Log("A key pressed!"); | |
} | |
// Thời điểm phím A được thả ra | |
if (aKey.wasReleasedThisFrame) | |
{ | |
Debug.Log("The A key has been released!"); | |
} | |
// Phím A có được nhấn hay không? | |
if (aKey.isPressed) | |
{ | |
Debug.Log("The A key is pressed!"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
public class MouseExample : MonoBehaviour | |
{ | |
private void Update() | |
{ | |
var current = Mouse.current; | |
if (current == null) return; | |
var mousePosition = current.position.ReadValue(); | |
var leftMouse = current.leftButton; | |
if (leftMouse == null) return; | |
if (leftMouse.wasPressedThisFrame) Debug.Log("OnMouseDown :" + mousePosition); | |
if (leftMouse.wasReleasedThisFrame) Debug.Log("OnMouseUp :" + mousePosition); | |
if (leftMouse.isPressed) Debug.Log("MousePressed :" + mousePosition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment