Last active
February 25, 2025 14:48
-
-
Save kurtdekker/506467ac3d78f36c8ff4b5c03b70d7eb to your computer and use it in GitHub Desktop.
Unity3D: rapidly swap old Input system for New Input System
This file contains 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
// | |
// @kurtdekker | |
// | |
// How to start using the new input system in place of the old one. | |
// | |
// If you've been using Input.* for everything, here is how to start | |
// using the new input system without a lot of extra fussing around. | |
// | |
using UnityEngine; | |
// you'll need this: | |
using UnityEngine.InputSystem; | |
// you'll need something like this in your Packages/manifest.json: | |
// | |
// "com.unity.inputsystem": "1.11.2", | |
// | |
public class Old2New : MonoBehaviour | |
{ | |
void Update() | |
{ | |
// holding a key down: | |
// Input.GetKey(KeyCode.LeftArrow); | |
if (Keyboard.current.leftArrowKey.IsPressed()) | |
{ | |
Debug.Log("Moving Left! " + Time.time); | |
} | |
// down / trigger / edge: | |
// Input.GetKeyDown(KeyCode.Space); | |
if (Keyboard.current.spaceKey.wasPressedThisFrame) | |
{ | |
Debug.Log("Fire! " + Time.time); | |
} | |
// click mouse button | |
// Input.GetMouseButtonDown(0) | |
if (Mouse.current.leftButton.wasPressedThisFrame) | |
{ | |
// mouse position | |
Vector2 position = Mouse.current.position.value; | |
Debug.Log("Click mouse Button 0: " + Time.time + " at " + position); | |
} | |
// hold mouse button | |
// Input.GetMouseButton(0) | |
if (Mouse.current.leftButton.IsPressed()) | |
{ | |
Debug.Log("Hold mouse Button 0: " + Time.time); | |
} | |
// This portion not yet tested: give it a whirl! | |
// multi-touch touchscreens | |
// Input.touches: | |
if (Touchscreen.current != null) | |
{ | |
var toucheControls = Touchscreen.current.touches; | |
string touchResult = ""; | |
foreach( var touchControl in toucheControls) | |
{ | |
var phase = touchControl.phase; | |
var value = touchControl.value; | |
touchResult += $"{phase} -> {value}, "; | |
} | |
if (touchResult.Length > 0) | |
{ | |
Debug.Log(touchResult); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment