Met*_*DoS 5 c# unity-game-engine
这篇文章是我在 Unity 论坛上的帖子的无耻的复制/粘贴:https : //forum.unity.com/threads/input-system-doesnt-trigger-anything-anymore.717386/,但 Stack Overflow 似乎更活跃
TL;DR:几天前 InputSystem 工作了,不要再触发任何东西了,呸。
几天前我尝试了新的输入系统,真是太棒了!我做了很多事情,试图了解使用它的最佳方式,最后,我有一个角色到处跳跃和移动,这很酷!然后,我将我的代码合并到我们的开发分支中并上床睡觉。
今天,我想继续我的代码,但我的角色不再移动,没有触发动作(即使在调试器中检测到输入),我真的不知道为什么。要么代码合并覆盖了一些重要的设置(我知道你在想什么,是的,“活动输入处理”设置在“两者”上,我尝试只运行预览)或者我在我的小测试中做了一些重要的事情,我没有意识到。
所以我决定尝试在一个全新的项目上重现我的步骤,也许你们能帮我弄清楚我做错了什么?
1/ 创建一个新的 2D 项目(通过 Hub)
2/安装最新的Package(0.9.0版)
4/ 重新启动 Unity Editor 因为它没有重新启动,即使消息说它会重新启动并检查项目设置(是的,它在“Both”上,是的,我的脚本运行时版本是 4.0)

5/ 创建一个新的 GameObject 并在其上添加一个 PlayerInput
6/点击“打开输入设置”并创建一个“输入设置”资产
7/ 点击“Create Actions...”来创建我的ActionMap资产
8/ 在我的“Player”ActionMap 上创建一个“TestAction”并将其设置为键“t”
9/ 创建一个新脚本“TestScript”,其中包含一个 OnTestAction() 方法(仅记录“测试”)并启用测试地图/操作(只是为了确定):
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.PlayerInput;
public class TestScript : MonoBehaviour
{
void Start()
{
InputActionMap playerActionMap = GetComponent<PlayerInput>().actions.GetActionMap("Player");
playerActionMap.Enable();
playerActionMap.GetAction("TestAction").Enable(); //Just to be sure
}
public void OnTestAction()
{
Debug.Log("test");
}
}
Run Code Online (Sandbox Code Playgroud)
10/ 像疯子一样按“播放”并发送垃圾邮件“T”以尝试显示调试(请注意,在调试器中,创建了一个用户,检测到我的“t”按下,我的 TestAction 存在并映射到“ t" 键但不显示调试

这可能是一个愚蠢的问题,但它让我发疯,我做错了什么?更气的是,前几天还管用!
附加信息: - 将输入管理从“两者”切换到“新输入系统(预览版)什么都不做 - 检查 Update() 是否启用了我的操作,每帧返回“True” - 检查 Update() 是我的操作被触发每一帧都返回“假” - 使用 action.started/triggered/performed 没有任何作用(我也尝试为此切换到 UnityEvent 或 C# 事件):
public class TestScript : MonoBehaviour
{
InputAction a;
void Start()
{
InputActionMap playerActionMap = GetComponent<PlayerInput>().actions.GetActionMap("Player");
playerActionMap.Enable();
a = playerActionMap.GetAction("TestAction");
a.Enable(); //Just to be sure
a.started += OnTriggeredTestAction;
a.performed += OnTriggeredTestAction;
a.canceled += OnTriggeredTestAction;
}
public void OnTestAction()
{
Debug.Log("test");
}
public void OnTriggeredTestAction(InputAction.CallbackContext ctx)
{
Debug.Log("test triggered");
}
}
Run Code Online (Sandbox Code Playgroud)
发布以防其他人遇到这个问题,因为这解决了我的问题。确保调用 Enable() 来启动路由事件。
//Create a and set the reference
private InputControls _inputMapping;
private void Awake() => _inputMapping = new InputControls();
//Route and Un-route events
private void OnEnable() => _inputMapping.Enable();
private void OnDisable() => _inputMapping.Disable();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6174 次 |
| 最近记录: |