当前位置:   article > 正文

Unity新版输入系统简单使用

unity新版输入系统

介绍之类的不在赘述,网上有许多教程.这里主要是怎么介绍快速调用

这里给出一个自认为不错的教程链接:Unity Input System 新输入系统的功能及用法介绍_unity 新输入系统_ZeryChen的博客-CSDN博客

一:直接输入

直接输入和旧版的用法差不多,直接上代码

  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
  4. using TouchPhase = UnityEngine.InputSystem.TouchPhase;
  5. public class DirectInput : MonoBehaviour
  6. {
  7. private Mouse mouse;
  8. private Keyboard keyboard;
  9. private Gamepad gamepad;
  10. private void Start()
  11. {
  12. //获取到游戏手柄之类的设备输入
  13. gamepad = Gamepad.current;
  14. //获取到键盘之类的设备输入
  15. keyboard = Keyboard.current;
  16. //获取到鼠标
  17. mouse = Mouse.current;
  18. //当输入设备改变时的回调方法
  19. InputSystem.onDeviceChange += (device, change) =>
  20. {
  21. switch (change)
  22. {
  23. case InputDeviceChange.Added:
  24. break;
  25. case InputDeviceChange.Disconnected:
  26. break;
  27. case InputDeviceChange.Removed:
  28. break;
  29. default:
  30. break;
  31. }
  32. };
  33. }
  34. private void Update()
  35. {
  36. //当前帧摁下了空格
  37. if (keyboard.spaceKey.wasPressedThisFrame)
  38. {
  39. Debug.Log("当前帧摁下了空格!");
  40. }
  41. //具有按钮或其他类型的输入的其他设备
  42. if (mouse.leftButton.isPressed)
  43. {
  44. Debug.Log("点击了鼠标左键!");
  45. }
  46. //是否摁下了a键
  47. if (Keyboard.current.aKey.isPressed)
  48. {
  49. Debug.Log("摁到了a键");
  50. }
  51. return;
  52. //针对移动平台的
  53. foreach (var touch in Touch.activeTouches)
  54. {
  55. if (touch.phase.Equals(TouchPhase.Began))
  56. {
  57. Debug.Log("手指触摸到了屏幕");
  58. }
  59. }
  60. }
  61. }

二:消息驱动响应(Send Messages Broadcast Messages--------------不建议使用)

1. 注意事项函数必须以On开头

2.必须与Player Input挂在同一个对象下

 

  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. // 此脚本必须与相关PlayerInput组件挂载到同一个GameObject
  4. public class MyMessages : MonoBehaviour
  5. {
  6. //对应的Move响应
  7. public void OnMove(InputValue inputValue)
  8. {
  9. Debug.Log(inputValue.Get<Vector2>());
  10. }
  11. }

三:事件驱动响应-UnityEvent(Invoke Unity Events)

1.新建一个脚本UnityEventInput 把脚本挂在随意一个物体上

2.写一个函数,触发Move时调用,其他Fire,Jump 什么自定义委托也是一样

  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class UnityEventInput : MonoBehaviour
  4. {
  5. public void GetCurrentObjMoveInfo(InputAction.CallbackContext callbackContext)
  6. {
  7. //获取到输入的值
  8. //Debug.Log(callbackContext.ReadValue<Vector2>());
  9. if (callbackContext.started)
  10. Debug.Log(transform.name + "开始");
  11. if (callbackContext.performed)
  12. Debug.Log(transform.position + "进行中");
  13. if (callbackContext.canceled)
  14. Debug.Log(transform.name + "结束输入了");
  15. }
  16. }

3.在Player Input 上赋值

 四:事件驱动响应-原生event(Invoke CSharp Events)

1.切换行为模式

2.找个物体挂上去实例出来 就能获取到值

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class CShaperEventInput : MonoBehaviour
  6. {
  7. private PlayerInput playerInput;
  8. private InputAction move;
  9. private InputAction fire;
  10. private void Start()
  11. {
  12. //PlayerInput可能有多个实例对象 用这个方法找,会按照场景中的物体从下往上找,也可以直接拖拽赋值 等等
  13. playerInput = PlayerInput.GetPlayerByIndex(0);
  14. // playerInput = FindObjectOfType<PlayerInput>();
  15. move = playerInput.actions["Move"];
  16. fire = playerInput.actions["Fire"];
  17. move.started += (parame) =>
  18. {
  19. Debug.Log("移动开始");
  20. };
  21. move.performed += (parame) =>
  22. {
  23. Debug.Log("移动进行中");
  24. };
  25. move.canceled += (parame) =>
  26. {
  27. Debug.Log("移动结束");
  28. };
  29. }
  30. private void Update()
  31. {
  32. //也可以每帧去读取移动的值
  33. Debug.Log(move.ReadValue<Vector2>());
  34. if (fire.IsPressed())
  35. {
  36. Debug.Log("开火中");
  37. }
  38. }
  39. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/958948
推荐阅读
相关标签
  

闽ICP备14008679号