【问题标题】:unity new input system invoke unity event triggers only one function only onceunity 新输入系统调用 unity 事件仅触发一次功能
【发布时间】:2022-09-25 23:00:16
【问题描述】:

我正在尝试使用新的输入系统来调用统一事件,但由于某种原因,在触发一个事件后,无法触发其他事件。 更准确地说,我正在尝试将输入系统与来自this video 的撤消/重做系统的略微修改版本相结合

首先,我使用了public static List<ICommand> Undoable;public static List<ICommand> Redoable;,而不是视频中显示的public static List<ICommand> CommandHistory

我尝试触发的功能示例: (这是来自测试类)

int i = 0;
public void Execute()
{
    Test();
}

private void Test()
{
    i++;
    Debug.Log($\"test No.{i}\"); 
}

public void Undo()
{
    Test();
}

我尝试从 CommandInvoker 使用

public void Test()
{
    CommandInvoker.AddCommand(new Test());
}

然后我尝试对其使用撤消:

public void Undo(InputAction.CallbackContext ctx)
{
    Debug.Log(CommandInvoker.Undoable.Count - 1);

    ICommand c = CommandInvoker.Undoable[CommandInvoker.Undoable.Count - 1];

    c.Undo();

    CommandInvoker.Undoable.Remove(c);

    CommandInvoker.Redoable.Add(c);
}

为了安全起见,我什至添加了 callbackContext ,但它根本没有帮助。

不知道代码有什么问题,所以加了几条调试日志:

Debug.Log($\"buffer count after adding command: {commandBuffer.Count}\");AddCommand()

Debug.Log($\"buffer count after execution: {commandBuffer.Count}\");update()

最后,Debug.Log($\"undoable actions: {Undoable.Count}\"); 在我将 c 添加到 Update() 的列表之后

这是我在控制台中得到的: 第一次调用测试操作:

添加命令后的缓冲区计数:1

添加命令后的缓冲区计数:2

测试一

执行后缓冲区计数:1

可撤消的操作:1

执行后的缓冲区计数:0

可撤消的操作:2

可撤销的操作:3 第二次调用测试动作:

可撤销的操作:4

可撤销的操作:5

可撤销的操作:6 调用撤消操作后:

-1

ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。 参数名称:索引

执行 \'keybinds and shortcuts/Undo[/Keyboard/ctrl,/Keyboard/z]\' 的 \'performed\' 回调时出现 ArgumentOutOfRangeException

-1

ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。 参数名称:索引

执行 \'keybinds and shortcuts/Undo[/Keyboard/ctrl,/Keyboard/z]\' 的 \'canceled\' 回调时出现 ArgumentOutOfRangeException

除了超出范围的错误,我将在解决第一个问题后尝试修复:测试操作只执行一次。

    标签: unity3d


    【解决方案1】:

    因此,无论如何,事实证明,关闭和打开计算机确实会对一些错误产生奇迹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 2022-07-20
      • 2020-12-27
      • 2023-03-06
      • 2022-01-16
      • 2020-09-29
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多