Unity iOS版本运行一段时间后UI无响应问题排查求助
Unity iOS构建版本事件系统无响应问题排查
问题描述
- 仅iOS构建版本出现异常:运行一段时间后按钮及事件触发器停止响应,看似Event System已不在层级中,但已实现自动重建Event System的专属函数
- Android版本无此问题,使用Unity版本为2022.3.22f1
日志信息
Xcode日志
NullReferenceException: Object reference not set to an instance of an object. at UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.Canvas canvas, UnityEngine.Camera eventCamera, UnityEngine.Vector2 pointerPosition, System.Collections.Generic.IList`1[T] foundGraphics, System.Collections.Generic.List`1[T] results) [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1[T] resultAppendList) [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine.EventSystems.EventSystem.RaycastAll (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1[T] raycastResults) [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine.EventSystems.PointerInputModule.GetTouchPointerEventData (UnityEngine.Touch input, System.Boolean& pressed, System.Boolean& released) [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine.EventSyste<…>
编辑器日志
MissingReferenceException: The object of type 'CanvasRenderer' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.Canvas canvas, UnityEngine.Camera eventCamera, UnityEngine.Vector2 pointerPosition, System.Collections.Generic.IList`1[T] foundGraphics, System.Collections.Generic.List`1[T] results) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/GraphicRaycaster.cs:317) UnityEngine.UI.GraphicRaycaster.Raycast (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1[T] resultAppendList) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/GraphicRaycaster.cs:218) UnityEngine.EventSystems.EventSystem.RaycastAll (UnityEngine.EventSystems.PointerEventData eventData, System.Collections.Generic.List`1[T] raycastResults) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:276) UnityEngine.EventSystems.PointerInputModule.GetMousePointerEventData (System.Int32 id) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/PointerInputModule.cs:295) UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent (System.Int32 id) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:548) UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:534) UnityEngine.EventSystems.StandaloneInputModule.Process () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/StandaloneInputModule.cs:282) UnityEngine.EventSystems.EventSystem.Update () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)
问题分析与解决方案
核心原因
编辑器日志明确提示CanvasRenderer被销毁后仍被访问,iOS平台因内存GC策略更激进、编译优化差异,表现为NullReferenceException,本质是UGUI射线检测时访问了已销毁的UI组件引用,并非EventSystem本身丢失(你的重建逻辑已排除此可能)。
修复步骤
- 规范UI销毁逻辑:不要单独销毁
CanvasRenderer组件,应销毁整个UI GameObject;销毁前移除所有关联的事件监听,将持有该UI对象的引用置为null。 - 添加射线检测防护:继承
GraphicRaycaster重写Raycast方法,在执行原逻辑前检查canvas、foundGraphics中的元素是否有效,跳过已销毁的对象。 - 升级Unity版本:2022.3 LTS分支后续版本(如2022.3.30f1及以上)修复过部分UGUI在iOS上的内存引用问题,可尝试升级验证。
- 优化EventSystem重建逻辑:重建EventSystem后,遍历所有Canvas的
GraphicRaycaster组件,调用SetDirty()或强制刷新其关联状态,确保射线检测链路正常。
是否为Bug?
这并非纯iOS系统Bug,而是UGUI在iOS特定内存环境下暴露的引用管理问题,编辑器已给出明确的错误提示,iOS的表现只是同一问题的不同形态。
内容的提问来源于stack exchange,提问作者Mario Rossi
相关产品推荐
相关产品推荐

