获取Unity输入系统UI模块的回调

huangapple go评论62阅读模式
英文:

Get callback of Unity Input System UI Module

问题

这是用于Unity中UI的输入系统

获取Unity输入系统UI模块的回调

我尝试捕获事件,就像我对另一个InputAction所做的那样:

void OnCancel(InputAction.CallbackContext context)
{
    //执行操作
}

但我从未收到这个回调。我该如何使用这个模块?

英文:

This is the Input System for UI in Unity

获取Unity输入系统UI模块的回调

I am trying to catch the events as i would do for another InputAction :

void OnCancel(InputAction.CallbackContext context)
{
     //Do things
}

But i never get this callback. How can i use this module ?

答案1

得分: 2

UI特定的默认操作与包含的DefaultInputActions资源一同提供。

Afaik,你可以通过以下方式简单地附加监听器,例如:

new DefaultInputActions().UI.OnCancel.performed += OnCancel;

如果你更倾向于使用自定义的输入操作资源,你也需要在其中进行配置。

你可以选择UI操作映射,然后复制它(CTRL C),然后进入你的自定义输入操作资源,将其粘贴到操作映射中(CTRL V)。

英文:

The UI specific default actions come shipped together with the DefaultInputActions asset shipped with the package containing

获取Unity输入系统UI模块的回调

Afaik you can attach listeners simply via e.g.

new DefaultInputActions().UI.OnCancel.performed += OnCancel;

If you are rather using your own custom Input Actions asset you need to also configure those in there.

You can select the UI action map and copy it (<kbd>CTRL C</kbd>) and then go into your custom Input Actions asset and paste it there into the actions maps (<kbd>CTRL V</kbd>).

答案2

得分: 1

Input System UI Input Module是Inputsystem和UI输入管理之间的桥梁,回调由内部管理。

从文档页面:

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.UI.InputSystemUIInputModule.html#UnityEngine_InputSystem_UI_InputSystemUIInputModule_cancel

指出:

> 从此输入生成的事件将由UnityEngine.EventSystems.ICancelHandler接收。

如果在您的MonoBehaviour上实现接口ICancelHandler和相应的OnCancel方法,当选择对象并触发取消操作时,应该看到调用该方法。

public class MyScript : MonoBehaviour, ICancelHandler
{
    // Your script code.

    public void OnCancel(EventSystems.BaseEventData eventData)
    {
        // 当取消事件发生时,此方法将被调用。
    }
}
英文:

The Input System UI Input Module is a bridge between the Inputsystem and the UI Input management, the callbacks are managed internally.

From the documentation page :

https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.UI.InputSystemUIInputModule.html#UnityEngine_InputSystem_UI_InputSystemUIInputModule_cancel

It is stated that:

> The events generated from this input will be received by UnityEngine.EventSystems.ICancelHandler.

If you implement the interface ICancelHandler on your MonoBehaviour and the respective OnCancel method, you should see the method getting called when your object is selected and a cancel action is triggered.

public class MyScript : MonoBehaviour, ICancelHandler
{
    // Your script code.

    public void OnCancel(EventSystems.BaseEventData eventData)
    {
        // This is getting called when a Cancel event occurs.
    }
}

huangapple
  • 本文由 发表于 2023年2月23日 23:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定