当前位置:网站首页>【Unity】InputSystem

【Unity】InputSystem

2022-07-05 00:28:00 A salted fish that likes playing games

Install the required plug-ins

version 1.3.0
 Unity At first, I didn't meet the current multi platform and various input devices , The design of the initial input system was difficult to meet many requirements, so InputSystsem.
Installation requirements :
 Unity2019.4 and .Net 4 Above version .
install :
  Go directly to the package manager Window > Package Manager Can be installed , Prompt for restart after installation , It can be used after restart .
Be careful :
1、InputSystem With part Unity The built-in functions are not yet compatible , Use as appropriate , Details refer to Official documents .
2、 If you want to switch the input system , You can go to Edit > Project Settings > Player stay Other Settings You can choose either of the two input methods in , It can also be used at the same time . Insert picture description here

Use

1、 establish Input Actions Components

 There are two ways to create :
1、 Right click Create-->Input Actions establish 

 Insert picture description here

2、 Mount the component on the object you want to control Player Input Back click Create Actions establish .

 Insert picture description here

2、 Mount the component on the object you want to control Player Input, Components as shown in the above figure , Then bind the corresponding Input Actions.

If you want to create a jump , Shooting and other actions , You can double-click to create Input Actions Or select this file and click Edit asset
 Insert picture description here
Enter the configuration interface
 Insert picture description here
Click the plus sign to create Action Map,Action And bind keys
 Insert picture description here
 Insert picture description here
As shown in the figure above ,Behavior There are many patterns , the Send Messages, Using this mode will call the methods in the information shown below .

jumping

Will SendMessage() to GameOject: OnDeviceLost, OnDeviceRegained,OncontrolsChanged, OnJump

So we want to trigger the event when the button is clicked , You need to create a method with the corresponding method name , Created a Player Script file and mount it on the object , Create the following script file :

using UnityEngine.InputSystem;// The namespace to be referenced 
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    
    public  void OnJump() 
    {
    
        transform.Translate(Vector3.up);
        Debug.Log(" jumping ");
    }
    
}

Successfully executed after running :
 Insert picture description here

Move

change Input Actions, Add one Action,
Action Type by Value,Control Type by Vector 2
Delete the binding below
 Insert picture description here
Right click to Action
 Insert picture description here
Create a combination of up, down, left and right
 Insert picture description here
Bind the corresponding key
Create the following script :

using UnityEngine.InputSystem;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    
    void OnMove(InputValue value)
    {
    
        Debug.Log(value.Get<Vector2>());
    }
}

Through the test, we can find that the output is :
 Insert picture description here
So the mobile code can be :

using UnityEngine.InputSystem;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    
    public float MoveSpeed;
    void OnMove(InputValue value)
    {
    
        transform.Translate(value.Get<Vector2>()*MoveSpeed*Time.deltaTime);
    }
}

But in this case, continuous movement cannot be achieved , Because the event can only respond once when the key is pressed .

原网站

版权声明
本文为[A salted fish that likes playing games]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141121133074.html