我正在使用新的输入系统 1.0.2 制作一款带有触摸、控制器和键盘输入的游戏
如果游戏当前正在运行并且开始按钮将被按下,那么 TouchUI 应该消失并且 PauseMenu 应该出现。目前它向我显示错误消息。这只发生在 TouchUICanvas 环境中。与游戏手柄和键盘完美配合。
一切正常。
错误警告:通过触摸开始按钮(TouchUICanvas)
-断言失败 UnityEngine.InputSystem.LowLevel。<>c__DisplayClass7_0:
b__0(NativeInputUpdateType,NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType,IntPtr)(位于/Users/builduser/buildslave/unity/build/Modules/Input/Private/Input.cs:120) -动态更新事件处理期间参数超出范围异常;重置事件缓冲区 UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:
b__0(NativeInputUpdateType,NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType,IntPtr)(位于/Users/builduser/buildslave/unity/build/Modules/Input/Private/Input.cs:120) -ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 参数名称:索引 UnityEngine.InputSystem.Utilities.InlinedArray`1[TValue].get_Item(System.Int32index)(atLibrary/PackageCache/[电子邮件受保护]/InputSystem/Utilities/InlinedArray.cs:68) UnityEngine.InputSystem.DynamicBitfield.ClearBit(System.Int32bitIndex)(atLibrary/PackageCache/[电子邮件受保护]/InputSystem/Utilities/DynamicBitfield.cs:51) UnityEngine.InputSystem.InputManager.FireStateChangeNotifications(System.Int32deviceIndex、System.DoubleinternalTime、UnityEngine.InputSystem.LowLevel.InputEventeventPtr)(atLibrary/PackageCache/[电子邮件受保护]/InputSystem/InputManager.cs:2924) UnityEngine.InputSystem.InputManager.UpdateState(UnityEngine.InputSystem.InputDevicedevice,UnityEngine.InputSystem.LowLevel.InputUpdateTypeupdateType,System.VoidstatePtr,System.UInt32stateOffsetInDevice,System.UInt32stateSize,System.DoubleinternalTime,UnityEngine.InputSystem.LowLevel.InputEventPtreventPtr)(atLibrary /PackageCache/[电子邮件受保护]/InputSystem/InputManager.cs:3101) UnityEngine.InputSystem.InputManager.UpdateState(UnityEngine.InputSystem.InputDevicedevice,UnityEngine.InputSystem.LowLevel.InputEventeventPtr,UnityEngine.InputSystem.LowLevel.InputUpdateTypeupdateType)(atLibrary/PackageCache/[电子邮件受保护]/InputSystem/InputManager.cs:第3017章) UnityEngine.InputSystem.InputManager.OnUpdate(UnityEngine.InputSystem.LowLevel.InputUpdateTypeupdateType,UnityEngine.InputSystem.LowLevel.InputEventBuffer&eventBuffer)(atLibrary/PackageCache/[电子邮件受保护]/InputSystem/InputManager.cs:2707) UnityEngine.InputSystem.LowLevel.NativeInputRuntime+<>c__DisplayClass7_0.
b__0(UnityEngineInternal.Input.NativeInputUpdateTypeupdateType,UnityEngineInternal.Input.NativeInputEventBuffer eventBufferPtr)(atLibrary/PackageCache/[电子邮件受保护]/InputSystem/NativeInputRuntime.cs:64) UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:b__0(NativeInputUpdateType,NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType,IntPtr)(位于/Users/builduser/buildslave/unity/build/Modules/Input/Private/Input.cs:120)
我在 ResumeButton(pauseScreen) 上按下 Touch - 并使用 PauseUnpause() 函数后,出现了这 2 个错误代码。
-操作已被触发,但显然不是来自交互,但绑定上有交互被触发?!?
UnityEngine.GameObject:SetActive(布尔值) OverworldPauseMenu:PauseUnpause()(位于 Assets/OverworldPauseMenu.cs:47) UnityEngine.EventSystems.EventSystem:Update() (位于 /Applications/Unity/Hub/Editor/2019.3.15f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
-控制索引超出范围 UnityEngine.GameObject:SetActive(Boolean) OverworldPauseMenu:PauseUnpause() (位于 Assets/OverworldPauseMenu.cs:47) UnityEngine.EventSystems.EventSystem:Update() (位于 /Applications/Unity/Hub/Editor/2019.3.15f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
PauseMenu 的代码:
public class OverworldPauseMenu : MonoBehaviour
{
public GameObject pauseScreen;
public GameObject TouchUICanvas;
public string mainMenu;
public bool isPaused;
ControlsSetup controls;
public void Awake()
{
controls = new ControlsSetup();
controls.SwitchPro.ButtonStart.performed += ctx => PauseUnpause();
}
void Start()
{
}
void Update()
{
}
public void PauseUnpause()
{
if (isPaused)
{
isPaused = false;
TouchUICanvas.SetActive(true);
pauseScreen.SetActive(false);
Time.timeScale = 1f;
}
else
{
isPaused = true;
TouchUICanvas.SetActive(false);
pauseScreen.SetActive(true);
Time.timeScale = 0f;
}
}
public void MainMenu()
{
SceneManager.LoadScene(mainMenu);
Time.timeScale = 1f;
}
void OnEnable()
{
controls.SwitchPro.Enable();
}
void OnDisable()
{
controls.SwitchPro.Disable();
}
}
请参阅下面的相关图片。
开始按钮:
控制设置:
控制台错误:
我通过延迟一帧按钮游戏对象禁用来“解决”这个问题。有点老套,但至少对我有用。
我建议使用 CanvasGroup 组件而不是 GameObject.SetActive。 您可以使用以下代码关闭显示。
canvasGroup.alpha = 0;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;