AB包的卸载高效稳定游戏开发的强制要求,它解决了资源管理中的内存泄漏问题,为动态的内容加载、热更新、大型世界的构建提供了内存保障,最终提升了游戏性能、稳定性和用户体验。
卸载资源
方式一(推荐使用):
卸载内存里的AB包,但场景里可以正常引用对应资源:
AssetBundle,UnLoad(false)
或
AssetBundle.UnLoadAllAssetBundles(false)
方式二:
卸载内存里的AB包同时也卸载场景里对AB包的引用关系:
AssetBundle.UnLoad(true)
或
AssetBundle.UnLoadAllAssetBundle(true)
AssetBundle包的获取注意
AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "cube");
ab.Unload(false);
//卸载后才能再次加载对应的AssetBundle
AssetBundle ab1 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "cube");
如下会报错:
AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "cube");
//卸载后才能再次加载对应的AssetBundle
AssetBundle ab1 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "cube");