Unity中AssetBundle加载的3D模型在Android不显示但编辑器正常
Unity Android AssetBundle加载成功但模型不显示的解决方案
核心问题分析
编辑器中正常、打包APK后失效,大概率是平台兼容性、路径适配、资源加载异常未捕获这几类问题,结合你清理AB名称、移除StreamingAssets文件的操作历史,以下是针对性排查步骤:
1. 确保AssetBundle的平台兼容性
- 必须为Android平台重新构建AssetBundle:编辑器默认用当前编辑器平台(Windows/Mac)构建的AB,在Android上会出现资源加载异常(即使AssetBundle对象不为null,
LoadAsset会返回null)。 - 构建步骤:打开
Build Settings切换到Android平台,再执行Build > Build AssetBundles,替换服务器上的旧AB文件。
2. 补全错误捕获与日志输出
你的代码中catch块为空,且未检查LoadAsset的返回值,导致资源加载失败时modelLoaded仍被设为true,无法定位问题。修改如下:
// 替换原catch块 catch (Exception ex) { Debug.LogError($"AB加载异常: {ex.Message}\n{ex.StackTrace}"); } // 在LoadAsset后添加检查 GameObject prefab = animal.LoadAsset<GameObject>(GetMethod.instance.clickedName); if (prefab == null) { Debug.LogError($"AB中找不到名为{GetMethod.instance.clickedName}的资源"); yield break; }
打包后通过adb logcat过滤Unity日志,就能看到具体错误(比如资源名拼写错误、资源未被打包进AB)。
3. 修复Android平台StreamingAssets路径访问
Android上Application.streamingAssetsPath指向APK内的压缩文件,无法直接用LoadFromFile加载,必须改用UnityWebRequest:
// 替换原本地AB加载代码 string streamingUrl = Path.Combine(Application.streamingAssetsPath, GetMethod.instance.clickedid); using (UnityWebRequest localRequest = UnityWebRequestAssetBundle.GetAssetBundle(streamingUrl)) { yield return localRequest.SendWebRequest(); if (localRequest.isHttpError || localRequest.isNetworkError) { Debug.LogError($"本地AB加载失败: {localRequest.error}"); yield break; } animal = DownloadHandlerAssetBundle.GetContent(localRequest); }
4. 恢复AssetBundle名称配置
之前清理未使用AB名称时,可能误删了当前AB的关联配置:
- 打开
Edit > Project Settings > Editor,找到AssetBundle Names列表,确认你的目标AB名称存在且正确关联了资源。 - 重新执行AssetBundle构建,确保所有依赖资源(纹理、模型组件)都被打包。
5. 检查实例化后的物体状态
- 旋转参数错误:你用
new Quaternion(0, 100, 0, 0)是错误的,四元数参数范围是-1到1,100会被归一化导致旋转异常,改用Quaternion.Euler(0, 100, 0)设置欧拉角。 - 相机可见性:检查实例化物体的位置
(0, -0.75f, 4)是否在相机视锥体内,Layer是否被相机的Culling Mask排除。 - 组件干扰:先注释
AddMissingComponents(fbxObject)方法,测试是否是该方法修改了渲染组件导致模型不显示。
修改后的完整协程代码
IEnumerator SpawnAnimal(string url) { using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url, 0)) { yield return request.SendWebRequest(); try { if (request.isHttpError || request.isNetworkError) { Debug.LogWarning("网络加载失败,尝试本地AssetBundle"); string streamingUrl = Path.Combine(Application.streamingAssetsPath, GetMethod.instance.clickedid); using (UnityWebRequest localRequest = UnityWebRequestAssetBundle.GetAssetBundle(streamingUrl)) { yield return localRequest.SendWebRequest(); if (localRequest.isHttpError || localRequest.isNetworkError) { Debug.LogError($"本地AB加载失败: {localRequest.error}"); yield break; } animal = DownloadHandlerAssetBundle.GetContent(localRequest); } } else { animal = DownloadHandlerAssetBundle.GetContent(request); } } catch (Exception ex) { Debug.LogError($"AB加载异常: {ex.Message}\n{ex.StackTrace}"); yield break; } if (animal != null) { GameObject prefab = animal.LoadAsset<GameObject>(GetMethod.instance.clickedName); if (prefab == null) { Debug.LogError($"AB中无资源: {GetMethod.instance.clickedName}"); yield break; } GameObject fbxObject = Instantiate(prefab); fbxObject.transform.position = new Vector3(0, -0.75f, 4); fbxObject.transform.rotation = Quaternion.Euler(0, 100, 0); // 修正旋转参数 // AddMissingComponents(fbxObject); // 先注释测试 modelLoaded = true; Debug.Log("模型实例化完成"); } else { Debug.LogError("AssetBundle加载为空"); } } yield return null; }
内容的提问来源于stack exchange,提问作者Arun P
相关产品推荐
相关产品推荐

