gpt4 book ai didi

c# - 如何使用新的输入系统访问点击(桌面)/点击(移动)位置?

转载 作者:行者123 更新时间:2023-12-03 09:34:58 26 4
gpt4 key购买 nike

我创建了一个新的 Unity 项目并安装了新输入系统的包。基本上我只想存储点击(桌面)/点击(移动)的位置,就是这样。
我知道旧系统提供解决方案

  • https://docs.unity3d.com/ScriptReference/Input-mousePosition.html
  • https://docs.unity3d.com/ScriptReference/Touch-position.html

  • 但我想用新的输入系统解决它。
    我从这个输入 map 配置开始(我将显示每个选定项目的配置)
    enter image description here
    enter image description here
    enter image description here
    enter image description here
    enter image description here
    enter image description here
    enter image description here
    我创建了一个记录每个点击/点击位置的新脚本
    public class FooBar : MonoBehaviour
    {
    public void Select(InputAction.CallbackContext context)
    {
    Vector2 selectPosition = context.ReadValue<Vector2>();
    Debug.Log($"Select position is: {selectPosition.x}|{selectPosition.y}");
    }
    }
    在场景中,我创建了一个空的游戏对象并在检查器中对其进行了配置
    enter image description here
    不幸的是,在运行播放模式时,每次移动鼠标时都会出现这些错误
    enter image description here
    这是第一条错误消息的堆栈跟踪
    enter image description here
    这是第二条错误消息的堆栈跟踪
    enter image description here
    所以我假设我的输入 map 配置是错误的。
    有人介意帮我设置一个输入配置,将点击/点击位置传递给脚本吗?

    因此,为了快速解决问题,我目前将此代码与旧输入系统一起使用,但我真的不喜欢它;)
        public sealed class SelectedPositionStateController : MonoBehaviour
    {
    private void Update()
    {
    #if UNITY_ANDROID || UNITY_IOS
    if (UnityEngine.Input.touchCount > 0)
    {
    Touch touch = UnityEngine.Input.GetTouch(0);

    // do things with touch.position
    }
    #elif UNITY_STANDALONE
    if (UnityEngine.Input.GetMouseButtonDown(0))
    {
    // do things with Input.mousePosition
    }
    #endif
    }

    // !!! USE THIS CODE BECAUSE IT'S OBVIOUSLY BETTER !!!
    //
    // public void SelectPosition(InputAction.CallbackContext context)
    // {
    // Vector2 selectedPosition = context.ReadValue<Vector2>();
    //
    // // do things with selectedPosition
    // }
    }

    最佳答案

    要达到预期的结果,您需要 2 InputAction .一个用于单击,另一个用于位置。
    您的 MonoBehaviour会听performed InputAction的事件与单击相关,并从与位置相关的位置读取位置。InputAction与职位相关的是可选的;因为您可以使用输入系统 API 捕捉到它。

  • Touchscreen.current.primaryTouch.position.ReadValue()
  • Mouse.current.position.ReadValue()

  • 对于您的用例,我建议:
  • 序列化 InputAction直接在MonoBehaviour
  • 使用 InputActionAsset并获取 InputAction从那里

  • 选项 1 - 直接输入 Action
    设置一 InputAction用于单击,另一个用于位置。
    然后您可能会听到 Click 并从 Position 中捕获位置。
    public class InputWithAction : MonoBehaviour
    {
    // --------------- FIELDS AND PROPERTIES --------------- //
    [SerializeField] private InputAction _click;
    [SerializeField] private InputAction _pos;

    // --------------- INITIALIZATION --------------- //
    private void Process(InputAction.CallbackContext callback)
    {
    //get the value from the position
    Debug.Log($"Input action: {_pos.ReadValue<Vector2>()}");
    //use the items below if you want to get the input directly
    #if UNITY_ANDROID || UNITY_IOS
    //gets the primary touch position using the new input system
    Debug.Log(Touchscreen.current.primaryTouch.position.ReadValue());
    #elif UNITY_STANDALONE
    //gets the current mouse position using the new input system
    Debug.Log(Mouse.current.position.ReadValue());
    #endif
    }

    // --------------- LISTENER SETUP --------------- //
    private void OnEnable()
    {
    _click.Enable();
    _pos.Enable();
    _click.performed += Process;
    }

    private void OnDisable()
    {
    _click.performed -= Process;
    _click.Disable();
    _pos.Disable();
    }
    }
    Click Action 需要 2 个绑定(bind)。
  • 使用 + 按钮添加投标 => 将路径设置为:鼠标/左键
  • 使用 + 按钮添加投标 => 将路径设置为:TouchScreen/Primary Touch/Tap

  • 位置需要 2 个绑定(bind)。
  • 使用 + 按钮添加投标 => 将路径设置为:鼠标/位置
  • 使用 + 按钮添加 Bidning => 将路径设置为:TouchScreen/Primary Touch/Position

  • 您的组件将如下所示:
    enter image description here
    选项 2 - InputActionAsset
  • 创建 InputMapAsset .
    从编辑器: Assets -> 创建 -> InputActions。称之为“你的 map ”
  • 删除所有 Action ,然后添加 2 个 Action 。

  • A.“点击” Action
    Action 类型:按钮
    绑定(bind)鼠标 => 鼠标/左键
    绑定(bind) TouchScreen => TouchScreen/Primary Touch/Tap
    B.“定位” Action
    操作类型:值
    绑定(bind)鼠标 => 鼠标/位置
    绑定(bind) TouchScreen => TouchScreen/Primary Touch/Position
    它看起来像这样:
    enter image description here
    您可以将“YourMap”引用为 InputActionAsset作为您的 MonoBehaviour 的一个字段并捕获 InputActionFindAction方法,使用字符串名称(“Click”和“Position”)。
        public class InputWithMap : MonoBehaviour
    {
    // --------------- FIELDS AND PROPERTIES --------------- //
    [SerializeField] private InputActionAsset _inputMap;
    private InputAction _click;
    private InputAction _pos;

    private void Start()
    {
    //enable is required only if you're not using PlayerInput anywhere else
    _inputMap.Enable();

    _click = _inputMap.FindAction("Click");
    _pos = _inputMap.FindAction("Position");

    //listen from clicks
    _click.performed += Process;
    }

    // --------------- INITIALIZATION --------------- //
    private void Process(InputAction.CallbackContext callback)
    {
    //get the value from the position
    Debug.Log($"Input action: {_pos.ReadValue<Vector2>()}");
    }

    private void OnDestroy() { _click.performed -= Process; }
    }

    一些考虑
  • 新的输入系统经过精心设计,设计人员可以使用 PlayerInput 完成所有操作。类,无需编码。结果不会像上面提供的解决方案那样高效。
  • 我将鼠标/触摸设置为单击一次。您还可以按住、双击或滚动添加 InteractionsBindings .
  • 理论上,您可能只使用一个 InputAction 就可以获得相同的结果。触摸屏使用 ReadOnlyArray<TouchControl>来自 TouchScreen类,但在这种情况下,鼠标和触摸会有不同的设置。阅读更多 MouseTouch支持。
  • 关于c# - 如何使用新的输入系统访问点击(桌面)/点击(移动)位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65681661/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com