英文:
Get callback of Unity Input System UI Module
问题
这是用于Unity中UI的输入系统
我尝试捕获事件,就像我对另一个InputAction
所做的那样:
void OnCancel(InputAction.CallbackContext context)
{
//执行操作
}
但我从未收到这个回调。我该如何使用这个模块?
英文:
This is the Input System for UI in Unity
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
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输入管理之间的桥梁,回调由内部管理。
从文档页面:
指出:
> 从此输入生成的事件将由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 :
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.
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论