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

AssetBundle.LoadAsset始终返回NULL,无法从资源包加载GameObject

解决AssetBundle加载GameObject返回NULL的问题

我来帮你定位并解决这个AssetBundle加载返回null的问题!结合你的代码和打包截图,咱们一步步排查:

1. 先确认AssetBundle里确实包含目标资源

你可以先通过打印bundle内所有GameObject的方式,验证"tv"是否真的在打包后的AssetBundle里。修改代码加入这段:

yield return www;
if (string.IsNullOrEmpty(www.error))
{
    AssetBundle bundle = www.assetBundle;
    // 打印bundle内所有GameObject,确认资源存在性
    var allGameObjects = bundle.LoadAllAssets<GameObject>();
    foreach (var go in allGameObjects)
    {
        Debug.Log($"AssetBundle内找到GameObject:{go.name}");
    }
    // 后续加载代码...
}

如果打印结果里没有"tv",说明打包时根本没把这个资源包含进去,那就要检查打包配置:

  • 选中你的tv预制体,在Inspector面板的「AssetBundle」下拉框,确认已经给它分配了正确的bundle名称(和你加载的bundle对应);
  • 打包时的BuildTarget要和你运行测试的平台一致(比如编辑器里测试就选StandaloneWindows),否则可能出现资源不兼容的情况。

2. 修正资源加载的方式

你的代码里用了强制类型转换,建议换成更稳妥的泛型重载方法,避免类型不匹配导致的null:

// 推荐写法1:泛型重载
GameObject tv = bundle.LoadAsset<GameObject>("tv");
// 推荐写法2:指定类型
GameObject tv = bundle.LoadAsset("tv", typeof(GameObject)) as GameObject;

同时要注意资源名称的大小写!Unity在部分平台(比如Linux、iOS)对文件名/资源名大小写敏感,如果你打包时的资源名是"TV",代码里写"tv"就会加载失败。

3. 检查AssetBundle加载完整性和生命周期

  • 确保www加载完成后再获取assetBundle,你的代码里已经用了yield return www;,这部分没问题,但可以加上www.isDone的判断更严谨;
  • 注意不要在加载资源前就调用bundle.Unload(true),这会直接释放bundle内的所有资源,导致加载返回null;
  • 加载完成后记得按需卸载bundle,避免内存泄漏:用完后可以调用bundle.Unload(false)(false表示保留已实例化的对象,true会卸载所有相关资源)。

修改后的完整示例代码

yield return www;
if (string.IsNullOrEmpty(www.error))
{
    AssetBundle bundle = www.assetBundle;
    
    // 先验证bundle内的资源
    var allGameObjects = bundle.LoadAllAssets<GameObject>();
    foreach (var go in allGameObjects)
    {
        Debug.Log($"找到资源:{go.name}");
    }
    
    // 加载并实例化tv
    GameObject tv = bundle.LoadAsset<GameObject>("tv");
    if (tv != null)
    {
        Instantiate(tv);
        Debug.Log("成功加载并实例化tv!");
    }
    else
    {
        Debug.LogError("无法从AssetBundle加载tv资源!");
    }
    
    // 按需卸载bundle(根据你的业务逻辑选择是否调用)
    // bundle.Unload(false);
}
else
{
    Debug.LogError($"加载AssetBundle出错:{www.error}");
}

内容的提问来源于stack exchange,提问作者Faiyaz Md Abdul

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:28:47