赞
踩
介绍之类的不在赘述,网上有许多教程.这里主要是怎么介绍快速调用
这里给出一个自认为不错的教程链接:Unity Input System 新输入系统的功能及用法介绍_unity 新输入系统_ZeryChen的博客-CSDN博客
直接输入和旧版的用法差不多,直接上代码
- using UnityEngine;
- using UnityEngine.InputSystem;
- using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
- using TouchPhase = UnityEngine.InputSystem.TouchPhase;
-
- public class DirectInput : MonoBehaviour
- {
- private Mouse mouse;
- private Keyboard keyboard;
- private Gamepad gamepad;
- private void Start()
- {
- //获取到游戏手柄之类的设备输入
- gamepad = Gamepad.current;
- //获取到键盘之类的设备输入
- keyboard = Keyboard.current;
- //获取到鼠标
- mouse = Mouse.current;
-
- //当输入设备改变时的回调方法
- InputSystem.onDeviceChange += (device, change) =>
- {
- switch (change)
- {
- case InputDeviceChange.Added:
-
- break;
- case InputDeviceChange.Disconnected:
-
- break;
- case InputDeviceChange.Removed:
-
- break;
- default:
- break;
- }
- };
-
- }
- private void Update()
- {
- //当前帧摁下了空格
- if (keyboard.spaceKey.wasPressedThisFrame)
- {
- Debug.Log("当前帧摁下了空格!");
- }
- //具有按钮或其他类型的输入的其他设备
- if (mouse.leftButton.isPressed)
- {
- Debug.Log("点击了鼠标左键!");
- }
- //是否摁下了a键
- if (Keyboard.current.aKey.isPressed)
- {
- Debug.Log("摁到了a键");
- }
- return;
- //针对移动平台的
- foreach (var touch in Touch.activeTouches)
- {
- if (touch.phase.Equals(TouchPhase.Began))
- {
- Debug.Log("手指触摸到了屏幕");
- }
- }
- }
-
- }

1. 注意事项函数必须以On开头
2.必须与Player Input挂在同一个对象下
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- // 此脚本必须与相关PlayerInput组件挂载到同一个GameObject
- public class MyMessages : MonoBehaviour
- {
- //对应的Move响应
- public void OnMove(InputValue inputValue)
- {
- Debug.Log(inputValue.Get<Vector2>());
- }
- }
1.新建一个脚本UnityEventInput 把脚本挂在随意一个物体上
2.写一个函数,触发Move时调用,其他Fire,Jump 什么自定义委托也是一样
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class UnityEventInput : MonoBehaviour
- {
- public void GetCurrentObjMoveInfo(InputAction.CallbackContext callbackContext)
- {
- //获取到输入的值
- //Debug.Log(callbackContext.ReadValue<Vector2>());
- if (callbackContext.started)
- Debug.Log(transform.name + "开始");
- if (callbackContext.performed)
- Debug.Log(transform.position + "进行中");
- if (callbackContext.canceled)
- Debug.Log(transform.name + "结束输入了");
- }
- }

3.在Player Input 上赋值
1.切换行为模式
2.找个物体挂上去实例出来 就能获取到值
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class CShaperEventInput : MonoBehaviour
- {
- private PlayerInput playerInput;
- private InputAction move;
- private InputAction fire;
- private void Start()
- {
- //PlayerInput可能有多个实例对象 用这个方法找,会按照场景中的物体从下往上找,也可以直接拖拽赋值 等等
- playerInput = PlayerInput.GetPlayerByIndex(0);
- // playerInput = FindObjectOfType<PlayerInput>();
- move = playerInput.actions["Move"];
- fire = playerInput.actions["Fire"];
-
-
- move.started += (parame) =>
- {
- Debug.Log("移动开始");
- };
- move.performed += (parame) =>
- {
- Debug.Log("移动进行中");
- };
- move.canceled += (parame) =>
- {
- Debug.Log("移动结束");
- };
-
- }
-
-
- private void Update()
- {
- //也可以每帧去读取移动的值
- Debug.Log(move.ReadValue<Vector2>());
-
- if (fire.IsPressed())
- {
- Debug.Log("开火中");
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。