Unity进阶----AssetBundle_02(加载依赖关系及网络资源)(2018/10/31)

网络资源加载:

 1 string path ="file://"+ Application.streamingAssetsPath + "\windows\123";
 2  string _path = "file://" + 
 3                Application.streamingAssetsPath + "\windows\ziyuab";
 4     using (WWW www = new WWW(path))
 5     {
 6         yield return www;
 7         using (WWW Xww = new WWW(_path))
 8         {          
 9             yield return Xww;
10             if (www != null && Xww != null)
11             {
12                 AssetBundle ab = www.assetBundle;
13                 AssetBundle et=  Xww.assetBundle;
14                 if (ab != null)
15                 {
16                     GameObject tempObj = ab.LoadAsset<GameObject>("Cube");
17                     Instantiate(tempObj);
18                 }
19             }
20             else
21             {
22                 Debug.Log("下载不成功!");
23             }
24         }
25     }
View Code

注意:定义路径如果是本地文件的话,在路径前加上"file://"

加载依赖关系:

 1   string ABPath;
 2   public string ABName;
 3   string bpath;
 4   List<AssetBundle> aBlist;
 5 void WanZheng()
 6     {
 7         aBlist=new List<AssetBundle>();
 8         ABPath = Application.streamingAssetsPath + "\windows\";
 9         bpath = Application.streamingAssetsPath + "/windows/windows";//依赖关系资源读取总路径
10         Yilai(bpath);//判断物体是否有依赖,有就加载出来,没有就不加载
11 #region 加载预制体,并实例化
12         AssetBundle _ab = AssetBundle.LoadFromFile(ABPath+ABName);//在此处_ab已将依赖加载出来了
13         GameObject obj = _ab.LoadAsset<GameObject>("Cube");
14         Instantiate(obj);
15 #endregion
16 
17 #region 释放内存
18         foreach (AssetBundle item in aBlist)
19         {
20             item.Unload(false);//清除依赖内存
21         }
22        _ab.Unload(true);//清除物体本身内存
23        AssetBundle _ab01 = AssetBundle.LoadFromFile(ABPath + ABName);//在此处_ab已将依赖加载出来了
24        GameObject obj01 = _ab01.LoadAsset<GameObject>("Cube");
25         Instantiate(obj01,Vector3.one,Quaternion.identity);
26 #endregion
27     }
28 void Yilai(string bpath)     
29     {
30         AssetBundle yilaiAB = AssetBundle.LoadFromFile(bpath);//读取依赖
31         AssetBundleManifest manifest 
32             = yilaiAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//读取依赖关系列表
33         string[] depndencies = manifest.GetAllDependencies(ABName);//按照名字加载名字旗下的依赖关系
34 
35         //将依赖放入列表并加载出来
36         if (depndencies.Length!=0)
37         {
38             foreach (string s in depndencies)
39             {
40                 aBlist.Add(loadDepndenciesByList(ABPath,s));
41             }
42         }
43     } 
44  private AssetBundle loadDepndenciesByList(string ABpath,string name) //加载依赖文件,按列表里的名字
45     {
46         return AssetBundle.LoadFromFile(ABpath+name);
47     }
View Code

笔记:

yield return xy;意思是线程中断,等待过程完成之后再进行.

截至目前学习存在的问题:

1). 实战项目中成百上千的大量资源需要(批量)打包处理,不可能手工维护方式给每个资源添加assetbundle包名称

2). Unity维护AssetBundle包的依赖关系不是很完善,主要体现在Unity仅仅维护包与包之间依赖关系的记录上。

(通过每个包创建的*.manifest文本文件实现)。如果要加载一个有多重依赖项的AssetBundle包,则要手工写代码,

把底层所有依赖包关系需要预先进行加载后才可以

3). AssetBundle包的商业应用涉及很多步骤: AB包的加载、 AB包依赖关系(要求: 不遗漏、不重复)、 资源的

提取与释放等。手工以及简单写代码实现功能,将是一项繁重海量工作,效率低下

原文地址:https://www.cnblogs.com/Future-Better/p/9883964.html