美文网首页
2018-05-09assetbundle

2018-05-09assetbundle

作者: a光头强_d237 | 来源:发表于2018-05-10 18:00 被阅读0次

    打包:打成ab包

    BuildPipeline.BuildAssetBundles(dir,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64)

    第一个参数是输出路径

    第二个参数如下

    BuildAssetBundleOptions.None 使用LZMA算法压缩,体积最小,加载时间最长

    BuildAssetBundleOptions.UncompressedAssetBundle 不压缩,体积最大,加载时间最短

    BuildAssetBundleOptions.ChunkBasedCompression 使用LZ4压缩 体积较小,加载时间较长(分块压缩分块加载;比如包里有abc 要加载a不用解压bc)

    第三个参数是平台选择(比如苹果安卓台式机)

    加载:加载ab包

    1,AssetBundle.LoadFromMemoryAsync 异步(异步需要等待加载完成)在内存里加载(先把文件读取到内存中(byte数组的形式),再从内存中读取)

    例:

    AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));

    yield return request;

    AssetBundle ab=request.assetB

    (同步的方式(不用协程):AssetBundle ab=AssetBundle.LoadFromMemory(File.ReadAllBytes(path));)

    GameObject wallPrefab=ab.LoadAsset<GameObject>("CubeWall");

    Instantiate(wallPrefab);

    2,AssetBundle.LoadFromFile,直接加载本地文件

    AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

    yield return request;

    AssetBundle ab = request.assetBundle;

    3,UnityWebRequest.GetAssetBundle(uri);从服务器加载资源

    UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);

    yield return request.Send();

    AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

    或者AssetBundle ab= (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;

    扩展:request.downloadHandler.data(下载的byte字节数组,可以通过File.WriteAllBytes保存到本地)

    加载:加载ab包中的资源

    1, T objectFromBundle = bundleObject.LoadAsset<T>(assetName);加载指定类型和名字的资源

    2,Unity.Object[] objectArray=loadedAssetBundle.LoadAllAssets();加载单个包里所有资源

    3,GameObject gameObject = loadedAssetBundle.LoadAsset<GameObject>(assetName);单个资源

    例:AssetBundleRequest request = loadedAssetBundleObject.LoadAssetAsync<GameObject>(assetName);

    yield return request;

    var loadedAsset = request.asset;

    加载:加载manifest文件

    AssetBundle manifestAB=AssetBundle.LoadFromFile("AssetBundles/AssetBundles");

    AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

    foreach(string name in manifest.GetAllAssetBundles()){

    print(name);所有ab包的名字

    }

    string[] strs = manifest.GetAllDependencies("cubewall.untiy3d");

    foreach(string name in strs){

    print(name);cubewall.untiy3d的依赖关系

    AssetBundle.LoadFromFile("AssetBundle/"+name);

    }

    string[] dependencies = manifest.GetAllDependencies("assetBundle");

    foreach(string dependency in dependencies){

    AssetBundle.LoadFromFile(Path.Combine(assetBundlePath,dependency));加载所有的依赖项

    }

    卸载:优化内存

    AssetBundle.Unload(true)卸载所有资源,即使有资源正在被使用(在适当的时候调用比如在关卡,场景切换的时候)推荐使用

    AssetBundle.Unload(false)卸载所有没有被使用的资源,个别的资源通过Resources.UnloadUnusedAssets(场景切换的时候会自动调用)卸载

    文件校验

    相关文章

      网友评论

          本文标题:2018-05-09assetbundle

          本文链接:https://www.haomeiwen.com/subject/djqnrftx.html