Unity new (Input System) old (Input Manager) input system code comparison

The following introductions are based on the Unity2022 version

1. Keyboard operation

When w key is pressed

//Old
        if (Input.GetKeyDown(KeyCode.W)) DoSomething();
//New
        if(Keyboard.current.wKey.wasPressedThisFrame) DoSomething();

When the w key is lifted

//Old
        if (Input.GetKeyUp(KeyCode.W)) DoSomething();
//New  
        if (Keyboard.current.wKey.wasReleasedThisFrame) DoSomething();

When the w key is pressed

//Old
        if (Input.GetKey(KeyCode.W)) DoSomething();
//New
        if (Keyboard.current.wKey.ReadValue() > 0.5f) DoSomething();

2. Mouse operation

Get mouse position

//Old
        Vector3 mousePos = Input.mousePosition;
//New
        mousePos = Mouse.current.position.ReadValue();

Get mouse wheel

//Old
        float scroll = Input.GetAxis("Scroll Wheel");
//New
        scroll = Mouse.current.scroll.ReadValue().y;

Get the left mouse button pressed

//Old
        if (Input.GetMouseButtonDown(0)) DoSomething();
//New
        if (Mouse.current.leftButton.wasPressedThisFrame) DoSomething();

Get the right mouse button lifted

//Old
        if (Input.GetMouseButtonUp(1)) DoSomething();
//New
        if (Mouse.current.rightButton.wasReleasedThisFrame) DoSomething();

Get the mouse middle click

//Old
        if (Input.GetMouseButton(2)) DoSomething();
//New
        if (Mouse.current.middleButton.ReadValue() > 0.5f) DoSomething();

Guess you like

Origin blog.csdn.net/ttod/article/details/128573068