AssetBundle使用总结

https://unity3d.com/fr/learn/tutorials/temas/best-practices/assetbundle-usage-patterns?_ga=2.236038465.1329205867.1508579438-695585774.1480855561#Managing_Loaded_Assets

UGUI打包策略
  • 一个UI模块对应一个图集
  • 一个UI模块的图集打一个AB包
  • 一个图集打成多个包加载后会造成冗余
  • 一个图集有多个Page,虽然打成了一个AB包,但加载后是多个贴图。但使用起来就像是一个贴图一样。
  • 一个UI模块的若干预设可以分别打包
  • 公共图集打若干AB包
注意
  • 一个AB包重复加载多次,则其中的Asset就会冗余几次
    所以要做好加载管理,避免AB的重复加载,同时也要避免资源的重复打包
资源卸载顺序
  • Destroy
  • ab.Unload(true)
  • Resources.UnloadUnusedAssets()
    如果在ab.Unload前执行此方法可能会无效,因为ab仍在引用assets。
其它
  • 如果我有一个Prefab,它的Dependencies都在Resources文件夹中,那么,当我在AssetBundle打包时,只打包这个Prefab(不指定BuildAssetBundleOptions.CompleteAssets和BuildAssetBundleOptions.CollectDependencies)的话,这个Prefab能够正确实例化吗?

这是不能正确实例化的,因为AssetBundle中的资源和Resource文件夹下资源是不会建立依赖关系的(脚本除外,因为开启BuildAssetBundleOptions.CollectDependencies 时,脚本依然不会打包到AssetBundle中)。所以会出现Mesh、Material等的丢失。

  • 获取所有AssetBundleName
var names = AssetDatabase.GetAllAssetBundleNames();
foreach (var name in names)
    Debug.Log ("AssetBundle: " + name);
  • 监听资源的AssetBundleName变化
using UnityEngine;
using UnityEditor;

public class MyPostprocessor : AssetPostprocessor {

    void OnPostprocessAssetbundleNameChanged ( string path,
            string previous, string next) {
        Debug.Log("AB: " + path + " old: " + previous + " new: " + next);
    }
}
  • Editor下可以加载任意平台下的AssetBundle,此外必须加载对应平台下的AssetBundle,否则会报以下错误
The file can not be loaded because it was created for another build target 
that is not compatible with this platform.
  • 通过AssetBundle加载的材质,Shader出现InternalErrorShader的情况
    • 被依赖的Shader没有在材质之前加载
    • 被依赖的Shader过早被Unload
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 翻译:莫铭原文地址:AssetBundle usage patterns 本系列中的上一篇文章覆盖了AssetBu...
    莫铭阅读 5,758评论 1赞 12
  • Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实...
    IongX阅读 1,813评论 0赞 5
  • 原文地址:http://gad.qq.com/article/detail/7180936作者:Loki+XUni...
    重装机霸阅读 8,603评论 0赞 41
  • 1、什么是AssetBundle AssetBundle 是Unity pro提供的一种用来存储资源的文件格式,它...
    好怕怕阅读 7,936评论 1赞 8
  • 如果我对一个人付出真心,却被那个人拒绝,我是不是傻?如果我把秘密告诉一个人,却被那个人告诉了其他人,我是不是纯?...
    一路繁花相送am阅读 263评论 0赞 0

友情链接更多精彩内容