由于某些原因,InputManager设置将被忽略,因此我实现了一个脚本来导航UI元素。有一个主意:
Selectable current
void setCurrent(Selectable selectable) {
current = selectable;
EventSystem.current.SetSelectedGameObject(current.gameObject, null);
}
void Update(){
if(current == null) {
setCurrent(Selectable.allSelectables[0]);
}
if(Input.GetKeyDown(KeyCode.UpArrow) && current.navigation.selectOnUp != null) {
setCurrent(current.navigation.selectOnUp);
}
}
当current
包含“按钮”或“切换”时有效,但是当InpuField导航中断并且日志显示每帧时出现以下错误。
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UI.InputField.ActivateInputFieldInternal () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:2213)
UnityEngine.UI.InputField.LateUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:725)
[编辑]
我确定current
永远不会为空。问题不是关于NullReferenceException
是什么,而是为什么某些源自Selectable的对象会导致该错误。我希望一些统一的开发人员可以重现此错误。