You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity跨平台切换后WebGL构建出现内存越界RuntimeError问题求助

Unity WebGL切换平台后内存越界报错解决方案

问题背景

项目最初基于Unity 2019.4.8f1开发,因该版本无法构建WebGL,升级至Unity 2021.3.7f1c1。初始WebGL构建正常,但切换至Android平台后再切回WebGL时,触发内存访问越界错误。

报错信息

exception thrown: RuntimeError: memory access out of bounds,RuntimeError: memory access out of bounds
at il2cpp::metadata::Il2CppTypeHash::Hash(Il2CppType const*) (http://localhost:8081/Build/****.wasm:wasm-function[947]:0xd70d4)
at google::sparsehash_internal::sh_hashtable_settings<KeyWrapper<Il2CppType const*>, il2cpp::metadata::Il2CppTypeHash, unsigned long, 4>::hash(KeyWrapper<Il2CppType const*> const&) const (http://localhost:8081/Build/****-webGL.wasm:wasm-function[1705]:0xfe369)
at google::dense_hashtable<std::__2::pair<KeyWrapper<Il2CppType const*> const, unsigned long>, KeyWrapper<Il2CppType const*>, il2cpp::metadata::Il2CppTypeHash, google::dense_hash_map<KeyWrapper<Il2CppType const*>, unsigned long, il2cpp::metadata::Il2CppTypeHash, KeyWrapper<Il2CppType const*>::EqualsComparer<il2cpp::metadata::Il2CppTypeEqualityComparer>, google::libc_allocator_with_realloc<std::__2::pair<KeyWrapper<Il2CppType const*> const, unsigned long> > >::SelectKey, google::dense_hash_map<KeyWrapper<Il2CppType const*>, unsigned long, il2cpp::metadata::Il2CppTypeHash, KeyWrapper<Il2CppType const*>::EqualsComparer<il2cpp::metadata::Il2CppTypeEqualityComparer>, google::libc_allocator_with_realloc<std::__2::pair<KeyWrapper<Il2CppType const*> const, unsigned long> > >::SetKey, KeyWrapper<Il2CppType const*>::EqualsComparer<il2cpp::metadata::Il2CppTypeEqualityComparer>, google::libc_allocator_with_realloc<std::__2::pair<KeyWrapper<Il2CppType const*> const, unsigned long> > >::find_position(KeyWrapper<Il2CppType const*> const&) const (http://localhost:8081/Build/****-webGL.wasm:wasm-function[1699]:0xfe1bb)
at google::dense_hash_map<KeyWrapper<Il2CppType const*>, unsigned long, il2cpp::metadata::Il2CppTypeHash, KeyWrapper<Il2CppType const*>::EqualsComparer<il2cpp::metadata::Il2CppTypeEqualityComparer>, google::libc_allocator_with_realloc<std::__2::pair<KeyWrapper<Il2CppType const*> const, unsigned long> > >::find(KeyWrapper<Il2CppType const*> const&) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[1633]:0xfab7f)
at il2cpp::vm::Reflection::GetTypeObject(Il2CppType const*) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[1616]:0xf8375)
at il2cpp::icalls::mscorlib::System::Object::GetType(Il2CppObject*) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[60633]:0xc13c0b)
at Object_GetType_mE10A8FC1E57F3DF29972CCBC026C2DC3942263B3 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[64061]:0xca0ae4)
at dynCall_iii (http://localhost:8081/Build/****-webGL.wasm:wasm-function[107999]:0x1cbada9)
at http://localhost:8081/Build/****-webGL.framework.js:1040:20
at invoke_iii (http://localhost:8081/Build/****-webGL.framework.js:14724:10)
at ABTestInstaller_InstallBindings_m3CF5B6CC950B2C0275B9E6BA7AA57BD49787D9CE (http://localhost:8081/Build/****-webGL.wasm:wasm-function[35956]:0x7c6e1e)
at InterfaceActionInvoker0::Invoke(unsigned short, Il2CppClass*, Il2CppObject*) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[66219]:0xce5894)
at dynCall_viii (http://localhost:8081/Build/****-webGL.wasm:wasm-function[108010]:0x1cbae46)
at http://localhost:8081/Build/****-webGL.framework.js:1040:20
at invoke_viii (http://localhost:8081/Build/****-webGL.framework.js:14746:3)
at Context_InstallInstallers_m01A84DFAE66F4462F24DE9A942296617222AE279 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49675]:0xa332ee)
at Context_InstallInstallers_m3AD29FF4EE2F625F3E1F82F91E7A7FA9A6E334E6 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49674]:0xa32950)
at SceneContext_InstallBindings_m3C7F585FCEAD32EEF9CDD66925E317A155875F98 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49774]:0xa399ec)
at dynCall_viii (http://localhost:8081/Build/****-webGL.wasm:wasm-function[108010]:0x1cbae46)
at http://localhost:8081/Build/****-webGL.framework.js:1040:20
at invoke_viii (http://localhost:8081/Build/****-webGL.framework.js:14746:3)
at SceneContext_Install_m8904B30F70C232AAC16EE134486ED63BEDB1D4E2 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49768]:0xa38955)
at SceneContext_RunInternal_m282A915AB634D9091851CBE0249903D9C6B39A5B (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49767]:0xa38352)
at RunnableContext_Run_m62FF1E74A523C4261857A042A0FBC43BC0D4A568 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49704]:0xa3451f)
at RunnableContext_Initialize_mE5D19AF80D494C5961C5675249414B90466B7016 (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49702]:0xa34454)
at SceneContext_Awake_mABEDDD9CF120B5BB7513552E9912A409CC7E59BC (http://localhost:8081/Build/****-webGL.wasm:wasm-function[49766]:0xa38340)
at RuntimeInvoker_TrueVoid_t4861ACF8F4594C3437BB48B6E56783494B843915(void (*)(), MethodInfo const*, void*, void**, void*) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[86201]:0x119d9bd)
at il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[88169]:0x11c50f6)
at dynCall_iiii (http://localhost:8081/Build/****-webGL.wasm:wasm-function[108002]:0x1cbadd8)
at http://localhost:8081/Build/****-webGL.framework.js:1040:20
at invoke_iiii (http://localhost:8081/Build/****-webGL.framework.js:14768:10)
at il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[88149]:0x11c4717)
at il2cpp_runtime_invoke (http://localhost:8081/Build/****-webGL.wasm:wasm-function[88207]:0x11c6638)
at scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[107070]:0x1c6e7b9)
at ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[105125]:0x1be16e3)
at ScriptingInvocation::InvokeChecked(ScriptingExceptionPtr*) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[105130]:0x1be18e7)
at SerializableManagedRef::CallMethod(Object&, ScriptingMethodPtr) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90738]:0x1243d16)
at MonoBehaviour::CallAwake() (http://localhost:8081/Build/****-webGL.wasm:wasm-function[105079]:0x1bddd74)
at MonoBehaviour::AddToManager() (http://localhost:8081/Build/****-webGL.wasm:wasm-function[105081]:0x1bde231)
at Behaviour::AwakeFromLoad(AwakeFromLoadMode) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90935]:0x12599c4)
at MonoBehaviour::AwakeFromLoad(AwakeFromLoadMode) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[105078]:0x1bddce9)
at AwakeFromLoadQueue::InvokePersistentManagerAwake(AwakeFromLoadQueue::Item*, unsigned int, AwakeFromLoadMode, bool) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[107132]:0x1c72050)
at AwakeFromLoadQueue::PersistentManagerAwakeFromLoad(ErrorsAndWarningsCapture*, bool) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[107129]:0x1c71350)
at LoadSceneOperation::CompleteAwakeSequence() (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90833]:0x124ee40)
at LoadSceneOperation::PlayerLoadSceneFromThread() (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90832]:0x124ea85)
at LoadSceneOperation::IntegrateMainThread() (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90830]:0x124d5c6)
at PreloadManager::UpdatePreloadingSingleStep(PreloadManager::UpdatePreloadingFlags, int) (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90845]:0x124feb0)
at PreloadManager::WaitForAllAsyncOperationsToComplete() (http://localhost:8081/Build/****-webGL.wasm:wasm-function[90846]:0x124ffea)
at main (http://localhost:8081/Build/****-webGL.wasm:wasm-function[102755]:0x1aa9186)
at http://localhost:8081/Build/****-webGL.framework.js:1040:20

已尝试操作

  • 拉取Git历史版本重新构建
  • 消除Unity IDE中所有错误,问题仍存在

当前构建设置

WebGL构建设置1
WebGL构建设置2

技术解决方案建议

1. 彻底清理Unity缓存与构建残留

  • 关闭Unity编辑器,删除项目目录下的Library、Temp、Obj、Build文件夹
  • 重新打开Unity,等待资源重新导入完成后,再次尝试构建WebGL

2. 重置WebGL平台构建设置

  • 进入File > Build Settings,切换到WebGL平台,点击Player Settings
  • 在Player Settings窗口中,点击右上角的Reset All按钮,恢复WebGL平台的默认设置
  • 重新配置项目所需的WebGL选项(如分辨率、压缩格式等)后构建测试

3. 检查IL2CPP相关配置

  • 在Player Settings的Player > Other Settings > Configuration中,确认Scripting Backend设置为IL2CPP
  • 切换IL2CPP Compiler Configuration的模式(Release/Debug),分别尝试构建
  • 开启Enable Exception Support选项,捕获更详细的错误堆栈,辅助定位问题

4. 排查第三方插件兼容性

  • 从报错堆栈看,错误触发于ABTestInstaller_InstallBindings方法,优先排查ABTest相关插件是否支持WebGL平台
  • 临时注释或移除ABTestInstaller相关代码,构建测试是否还会报错,确认是否为插件导致的问题
  • 检查项目中其他依赖反射、IL2CPP钩子的插件,逐一排查兼容性

5. 升级Unity补丁版本

  • Unity 2021.3.7f1c1存在已知的WebGL与IL2CPP平台切换内存BUG,建议升级至2021.3.x系列的最新稳定补丁(如2021.3.28f1),修复已知问题

6. 调整WebGL内存分配

  • 在Player Settings的WebGL > Memory Size中,尝试增大内存限制(例如从默认256MB调整为512MB),避免内存不足引发的越界访问

内容的提问来源于stack exchange,提问作者Initial_Equationor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 02:54:24