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

Unity Vuforia编译Android时触发IndexOutOfRangeException数组越界错误求助

Unity Android编译Vuforia时触发IndexOutOfRangeException错误的解决思路

问题概述

编译Unity项目到Android平台时,因Vuforia引擎的Gradle属性解析逻辑抛出数组越界异常,错误堆栈如下:

IndexOutOfRangeException: Index was outside the bounds of the array.
Vuforia.EditorClasses.GradlePropertiesParser+<>c.<FromString>b__3_1 (System.String v) (at <e2ac827c108347a08067e2bd69c6ad84>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (TSource[] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) (at <7bdabb6dfbaa4804838c76fbb93968d4>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector, System.Collections.Generic.IEqualityComparer`1[T] comparer) (at <7bdabb6dfbaa4804838c76fbb93968d4>:0)
System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] keySelector, System.Func`2[T,TResult] elementSelector) (at <7bdabb6dfbaa4804838c76fbb93968d4>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromString (System.String content) (at <e2ac827c108347a08067e2bd69c6ad84>:0)
Vuforia.EditorClasses.GradlePropertiesParser.FromFile (System.String path) (at <e2ac827c108347a08067e2bd69c6ad84>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.ParseGradleProperties (System.String projectPath) (at <e2ac827c108347a08067e2bd69c6ad84>:0)
Vuforia.EditorClasses.PostProcessAndroidBuild.OnPostGenerateGradleAndroidProject (System.String path) (at <e2ac827c108347a08067e2bd69c6ad84>:0)
UnityEditor.Android.PostProcessor.Tasks.ProcessGenerateProjectCallbacks.OnGeneratePlatformProjectPostprocess (System.String path, System.Boolean strict) (at <3c54bb7f2c3e4782b3adbd9c545b57b8>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

该问题在Vuforia官方仓库相关Issue中有提及但无明确解决方案,以下是实操性修复步骤:

解决步骤

1. 检查并修复gradle.properties文件格式

异常根源是Vuforia解析gradle.properties时遇到不符合key=value格式的行,导致分割数组越界:

  • 打开项目根目录的gradle.properties文件
  • 删除空行、无=的无效行、格式混乱的注释行
  • 确保所有配置项严格遵循键=值的格式,无多余空格或特殊字符

2. 重置Unity缓存与Vuforia配置

  • 关闭Unity编辑器,删除项目目录下的Library文件夹
  • 重新打开项目,等待Unity重新生成所有缓存文件
  • 确认Vuforia包版本与当前Unity版本兼容,若不兼容则升级/降级到匹配版本

3. 手动修正Vuforia的Gradle解析逻辑(进阶)

若上述方法无效,可修改Vuforia的解析代码处理异常行:

  • 找到Vuforia包中的GradlePropertiesParser.cs文件(通常在Packages/com.ptc.vuforia.engine/Editor目录下)
  • 在FromString方法中添加对空行、无效行的过滤逻辑,示例:
    var lines = content.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
    var validLines = lines.Where(line => !string.IsNullOrWhiteSpace(line) && line.Contains('='));
    // 后续用validLines进行字典转换
    

内容的提问来源于stack exchange,提问作者Liban Valladares Martel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 04:02:03