Unity进阶----AssetBundle_01(2018/10/30)

 

 AssetBundle作用和定义

1).AssetBundle是一个压缩包包含模型、贴图、预制体、声音、甚至整个场景,可以在游戏运行的时候被加载;

2).AssetBundle自身保存着互相的依赖关系;

AssetBundle使用相关API:

BuildPipeline.BuildAssetBundles(_path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);

AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/scene/wall.unity3d");

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

AssetBundle本地文件压缩加载使用代码:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Loding : MonoBehaviour {   
 6     void Start ()
 7     {
 8        string _abPath = Application.streamingAssetsPath + "\windows\123";       
 9        string _bpath = Application.streamingAssetsPath + "\Windows\ziyuab";//依赖关系资源加载路径
10       AssetBundle xt = AssetBundle.LoadFromFile(_abPath);
11       AssetBundle.LoadFromFile(_bpath);// 依赖关系资源加载
12       if (xt == null)
13       {
14           Debug.Log("加载ab出错!!!");
15           return;
16       }
17       GameObject tempObj = xt.LoadAsset<GameObject>("Cube");
18       if (tempObj != null)
19       {
20           Instantiate(tempObj);
21       }
22       else
23       {
24           Debug.Log("加载该物体出错!!!");
25       }
26      StartCoroutine("Creat",xt);
27     
28     }
29     void Update () 
30     {
31         
32     }
33     //携程是用来说明qw.Unload是释放内存的作用
34     IEnumerator Creat (AssetBundle qw)
35     { 
36        yield return new WaitForSeconds(3);
37         qw.Unload(false);//false无变化,true时移出身上的东东(项目)
38         Debug.Log("begin");
39       yield return new WaitForSeconds(10);
40       Instantiate(qw.LoadAsset<GameObject>("Sphere") );//报错是因为内存删除ab包
41     }
42 }
View Code

ab.Unload(true); --> 从项目和内存中都干掉

ab.Unload(false); --> 只干掉内存

知识点

UI 都采用动态绑定

WWW www = new WWW(ABURL)
AssetBundle ab = www.assetBundle;

AB包 是一个特殊格式的压缩包 关于unity资源都能压缩 不包含代码的 UI 模型 音乐

热更新: 不干掉原来的进行更新(打补丁)

1. 进行AB包的时候进行属性设置
2. 将设置好的属性(预制体...) 压缩成为一个特定的文件 代码压缩的!!!
3. 放置到web/gameserver上面
4. 客户端经过检验之后下载
5. 下载之后解压实例化

编译器扩展: BuildAssetBundleTool

1. 在editor目录下

2. AssetBundle的名字就是压缩包的名字和实质内容基本无关!

3.代码:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEditor;
 5 public class BuildTools  
 6 {
 7     [MenuItem("AssetBulidTools/BuildAB")]//建立编译器扩展
 8    public static void buliding()
 9    {
10        string _ABpath = Application.streamingAssetsPath + "\windows";
11         //检测是否存在
12        BuildPipeline.BuildAssetBundles(_ABpath,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
13        //这两行代码自动检测有AssetBundle标签的预制体,图片,音源等,并进行资源压缩.
14    }
15 }
View Code
原文地址:https://www.cnblogs.com/Future-Better/p/9876965.html