• Unity中AssetBundle学习


    AssetBundle是将游戏中所需的各类资源打包压缩并上传到网络服务器上。在游戏运行时从服务器上将这些资源动态加载到客户端。

    AssetBundle的使用步骤   1:指定资源的AssetBundle属性(给资源命名,分类,指定后缀名)

                                             2:使用代码构建AssetBundle资源包

                                             3:把AssetBundle资源包上传到服务器上

                                             4:从服务器上动态加载AssetBundle资源

    1:指定资源的AssetBundle属性(给资源命名,分类,指定后缀名):在Project 面板上选中资源,指定资源的AssetBundle资源属性,对资源进行分组、命名和指定后缀名

          指定资源AssetBundle的属性的注意事项 1:在指定资源的名字时,也可以指定资源存放的相对文件夹,如(scenes/cube)把cube放在AssetBundles文件夹下的Scenes文件夹下,                                                                             secnes文件夹不需要创造。

                                                                          2:文件的后缀名是自己命名的。

                                                                          3:Remove Unused Name去除一些资源的名字

         资源分组的策略:1 按逻辑分类

                                      2 按照资源分类

                                        一类资源放一个资源包中,如所有的精灵,所有的声音等

                                      3 按照使用分类

                                         比如一个关卡一个类,后面的关卡可以一个一个加载

                                      总结分组策略:

                                       1: 经常要更新的资源放在一个包上,不经常更新的资源放入一个包。

                                       2:需要同时更新的资源放一个包

                                       3:项目中的共享资源放一个包,在使用资源前,要先加载共享资源。

                                       4:同一时间使用的资源放入一个包。

                                       5:图片精灵要放在一个包中。因为一个图片精灵就会生成一个图集,所有的图片精灵也只会生成一个图集。

    2:使用代码构建AssetBundle资源包:构建一个扩展编辑器,让其实现构建AssetBundle资源包的功能

                                                                  Project---->create  Editor文件夹,创建脚本,脚本放在Editor文件夹下,不需挂在物体上,脚本要实现扩展编辑器,和构建AssetBundle资源包                                                               的功能

      代码:

               Using System.IO;

               Using Unity.Editor;

              public   class CreateAssetBundles()

             {

                 [MenuItem("Assets/BuildAssetBundles")]//扩展编辑器,在Assets窗口下,创建BuildAssetsBundle按钮,绑定了BuildAllAssetBundles方法。

                 Static void BuildAllAssetBundles()//实现资源打包的功能

                {

                  BulidPipeLine.BuildAssetBundles(path,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindow64);把设置过AssetBundle属性的资源按JZMA算法打包成Windows格               式的文件,把这些文件放到到指定文件夹上,

               }

             }

    代码:

    using System.IO;//文件操作的命名空间
    using UnityEditor;//编辑器扩展的命名空间
    
    
    public class CreateAssetBundles {
        [MenuItem("Assets/BuildAssetsBundles")]//编辑器扩展
        static void BuildAllAssetBundles()
        { 
            string str = "AssetBundles";//指定文件夹的相对路径
            if(Directory.Exists(str)==false)//判断文件夹是否存在
            {
                Directory.CreateDirectory(str);//不存在就创建该文件夹
            }
            BuildPipeline.BuildAssetBundles(str,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);//对设置过AssetBundle属性的资源进行打包。
        }
    
    
    }

    AssetBundle资源包的分析:

    一个资源被AssetBundle后会生成一个存放资源的文件。一个以.manifest为后缀的文件,这个文件记录资源的信息,如文件的CRC校验码,资源打包前的存放位置Asste,物体依靠的其他资源Dependencies

    BuildPipeLine,BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None, BuildTarget targetPlatform = BuildTarget.WebPlayer);方法的参数分析

              OutPutPath:打包的资源存放的文件夹,该文件夹必须存在。

              BuildAssetBundleOptions 资源打包的压缩格式

                          BuildAssetBundleOptions.None:使用默认的打包格式,用LZMA算法进行压缩。使用LZMA算法压缩的资源,压缩包小,在加载是需要的时间比较长(解压),在使用                                                                             压缩包时,要把包内所有的资源全部解压,解压后的资源要用LZ4重新压缩,(使用LZ4算法压缩的资源可以使用谁再解压谁)

                          BuildAssetBundleOptions.UnCompressedAssetBundle:资源不压缩,资源包大,但加载快

                          BuildAssetBundleOptions.ChunkBaseCompressed:使用LZ4算法进行压缩,使用的资源可以使用谁,再加载谁。

              BuildTarget 资源打包到什么平台 (Windows/IOS/Android.......)

                          

    4:动态加载资源到AssetBundle对象中

          1 从本地加载资源到AssetBundle对象中 

              同步加载      AssetBundle ab=   AssetBundle.LoadFromFile(Path),Path要指定到具体哪个文件,文件后缀名要写

              

    void Start () {
            AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/perfabs/Sphere.unity");//把文件从本地放入AssetBundle对象中
            GameObject go = ab.LoadAsset<GameObject>("Sphere");//把文件中Sphere物体从AssetBundle中加载出来
            GameObject.Instantiate(go);//在场景中创建该物体
        }

              异步加载  AssetBundleCreateRequest   request= AssetBundle.LoadFromFileSync(Path)

          

    void Start () {
           // AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/perfabs/cylinder.unity");//把文件从本地放入AssetBundle对象中
            //GameObject go = ab.LoadAsset<GameObject>("cylinder");//把文件中Sphere物体从AssetBundle中加载出来
           // GameObject.Instantiate(go);//在场景中创建该物体
            StartCoroutine("CreateAssetBundle");//开启协程
        }
        IEnumerator CreateAssetBundle()
        {       
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync("AssetBundles/perfabs/cylinder.unity");//异步加载本地资源
            yield return request; //等待加载完成      
            AssetBundle ab = request.assetBundle;//将资源放入AssetBundle对象中
            GameObject go = ab.LoadAsset<GameObject>("cylinder");//把文件中cylinder物体从AssetBundle中加载出来
            GameObject.Instantiate(go);//在场景中创建该物体
        }

        注: StartCoroutine("Functions");//开启协程

                EndCoroutine("Functions");//关闭协程   

          2 从服务器中加载资源到AssetBundle中

               1用www类,可以从本地(file:///地址)或服务器中下载AssetBundle资源(http://地址)

                     WWW www=WWW.LoadFromCacheOrDownLoad(path,版本)

                    代码:

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
            while(Caching.ready==false)
            {
                yield return null;//内存未准备好就再等一帧
            }
          // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
           string path1 = @"http://localhost/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
           // string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";
            WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            yield return WWW1;//等待加载完成
            if(string.IsNullOrEmpty(WWW1.error))//如果www1的错误为空
            {
                AssetBundle ab = WWW1.assetBundle;
                GameObject go = ab.LoadAsset<GameObject>("sphere");//将资源sphere加载到Unity中
                GameObject.Instantiate(go);//在场景中加载该物体
            }
            else//如果www1的错误不为空
            {
                Debug.Log("加载出错");
            }
    
            
    
    
        }

                   2 UnityWebRequest类下载   UnityWebRequest request=UnityWebRequest.GetAssets(path)

                  代码:

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
           
         // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
            string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
            UnityWebRequest request = UnityWebRequest.GetAssetBundle(path3);//从服务器中下载资源
            // WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            //yield return WWW1;//等待加载完成
            yield return request.SendWebRequest();//等待资源下载完毕
            if (request.isNetworkError || request.isHttpError)//判断是否有下载错误
            {
                Debug.Log("error");
            }
            else
            {
                AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
     AssetBundle ab1 = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//功能相同 GameObject go
    = ab.LoadAsset<GameObject>("sphere");//加载特定的资源到游戏中 Instantiate(go);//将资源再场景中加载出来 }

          3 从内中加载资源到AssetBundle中

           同步加载      AssetBundle ab=   AssetBundle.LoadFromMemory(Path),Path要指定到具体哪个文件,文件后缀名要写

    void Start () {
            // StartCoroutine("CreateAssetBundle");//开启协程  
            
                AssetBundle ab = AssetBundle.LoadFromMemory( File.ReadAllBytes( "AssetBundles/perfabs/Sphere.unity"));//先把文件读到内存中(File.ReadAllBytes(path)),再把内存中的文件放入AssetBundle对象中
                GameObject go = ab.LoadAsset<GameObject>("Sphere");//把文件中Sphere物体从AssetBundle中加载出来
                GameObject.Instantiate(go);//在场景中创建该物体
            
        }

        异步加载  AssetBundleCreateRequest   request= AssetBundle.LoadFromMemorySync(Path)

      

    void Start () {
             StartCoroutine("CreateAssetBundle");//开启协程         
        }
        IEnumerator CreateAssetBundle()
        {
            AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/perfabs/Sphere.unity"));
            yield return request;
            AssetBundle ab = request.assetBundle;
            GameObject go = ab.LoadAsset<GameObject>("Sphere");
            GameObject.Instantiate(go);//在场景中创建该物体
              
        }

    5:动态加载使用AssetBundle对象中的资源

          1 使用从本地加载AssetBundle资源到游戏中 

          AssetBundle  public  Functions  :1 T go=  ab. LoadAsset<T>(name) //同步加载AssetBundle对象中的资源的指定资源

                                                                 2 object[]  obj= ab.LoadAssets()//同步加载AssetBundle对象中的全部资源资源

                                                                 3  AssetBundleRequest request=ab.loadAssetAsync<T>(name);//异步加载AssetBundle对象中的资源的指定资源

                                                                 4  AssetBundleRequest request=ab.loadAssetsAsync<>;//异步加载AssetBundle对象中的所有资源

        同步加载AssetBundle对象中的资源的指定资源  

     AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/perfabs/sphere.unity");//把文件从本地放入AssetBundle对象中
           GameObject go = ab.LoadAsset<GameObject>("sphere");//把文件中Sphere物体从AssetBundle中加载出来
           GameObject.Instantiate(go);//在场景中创建该物体

     同步加载AssetBundle对象中的全部资源资源

      AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/perfabs/sphere.unity");//把文件从本地放入AssetBundle对象中
            Object[] obj = ab.LoadAllAssets();//把文件中所有的资源从AssetBundle中加载出来
            foreach(Object ob in obj)
            {
               GameObject.Instantiate(ob);//遍历创建这些资源
            }

    异步加载AssetBundle对象中的资源的指定资源 

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程
        }
        IEnumerator CreateAssetBundle()
        {       
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync("AssetBundles/perfabs/cylinder.unity");//异步加载本地资源
            yield return request; //等待加载完成      
            AssetBundle ab = request.assetBundle;//将资源放入AssetBundle对象中
            AssetBundleRequest request1 = ab.LoadAssetAsync <GameObject>("cylinder");//把文件中cylinder物体从AssetBundle中加载出来
            yield return request1;
            GameObject go = request1.asset as GameObject;
            GameObject.Instantiate(go);
        }

          异步加载AssetBundle对象中的所有资源

         

        void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程      
        }
        IEnumerator CreateAssetBundle()
        {       
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync("AssetBundles/perfabs/cylinder.unity");//异步加载本地资源
            yield return request; //等待加载完成      
            AssetBundle ab = request.assetBundle;//将资源放入AssetBundle对象中
            AssetBundleRequest request1 = ab.LoadAllAssetsAsync();//把文件中的所有资源异步从AssetBundle中加载出来
            yield return request1;//等待资源加载完成
            Object[] obj = request1.allAssets;//资源对象
            foreach (Object ob in obj)
            {
                GameObject.Instantiate(ob);//遍历创建这些资源
            }     
        }

          2 使用从服务器中加载AssetBundle资源到游戏中

                

          AssetBundle  public  Functions  :1 T go=  ab. LoadAsset<T>(name) //同步加载AssetBundle对象中的资源的指定资源

                                                                 2 object[]  obj= ab.LoadAssets()//同步加载AssetBundle对象中的全部资源资源

                                                                 3  AssetBundleRequest request=ab.loadAssetAsync<T>(name);//异步加载AssetBundle对象中的资源的指定资源

                                                                 4  AssetBundleRequest request=ab.loadAssetsAsync<>;//异步加载AssetBundle对象中的所有资源

                                1用www类,可以从本地(file:///地址)或服务器中下载AssetBundle资源(http://地址)

                                  同步加载AssetBundle对象中的资源的指定资源   

                                               

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
            while(Caching.ready==false)
            {
                yield return null;//内存未准备好就再等一帧
            }
          // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
           string path1 = @"http://localhost/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
           // string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";
            WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            yield return WWW1;//等待加载完成
            if(string.IsNullOrEmpty(WWW1.error))//如果www1的错误为空
            {
                AssetBundle ab = WWW1.assetBundle;
                GameObject go = ab.LoadAsset<GameObject>("sphere");//将资源sphere加载到Unity中
                GameObject.Instantiate(go);//在场景中加载该物体
            }
            else//如果www1的错误不为空
            {
                Debug.Log("加载出错");
            }
    
            
    
    
        }

                        异步加载AssetBundle对象中的资源的指定资源   

        void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
            while(Caching.ready==false)
            {
                yield return null;//内存未准备好就再等一帧
            }
          // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
           string path1 = @"http://localhost/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
           // string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";
            WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            yield return WWW1;//等待加载完成
            if(string.IsNullOrEmpty(WWW1.error))//如果www1的错误为空
            {
                AssetBundle ab = WWW1.assetBundle;
                AssetBundleRequest request = ab.LoadAssetAsync<GameObject>("sphere");//异步加载Sphere资源
                yield return request;//等待资源加载完成
                GameObject go = request.asset as GameObject;//将资源转成Gameobject类型
               // GameObject go = ab.LoadAsset<GameObject>("sphere");//将资源sphere加载到Unity中
                GameObject.Instantiate(go);//在场景中加载该物体
            }
            else//如果www1的错误不为空
            {
                Debug.Log("加载出错");
            }
    
            
    
    
        }

                      同步加载AssetBundle对象中的全部资源资源

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
            while(Caching.ready==false)
            {
                yield return null;//内存未准备好就再等一帧
            }
          // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
           string path1 = @"http://localhost/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
           // string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";
            WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            yield return WWW1;//等待加载完成
            if(string.IsNullOrEmpty(WWW1.error))//如果www1的错误为空
            {
                AssetBundle ab = WWW1.assetBundle;
                //  AssetBundleRequest request = ab.LoadAssetAsync<GameObject>("sphere");//异步加载Sphere资源
                //  yield return request;//等待资源加载完成
                // GameObject go = request.asset as GameObject;//将资源转成Gameobject类型
                Object[]  go = ab.LoadAllAssets();//同步加载所有资源
                foreach(var item in go)
                {
                    GameObject.Instantiate(item);//在场景中加载该物体
    
                }
            }
            else//如果www1的错误不为空
            {
                Debug.Log("加载出错");
            }
    
            
    
    
        }

         异步加载AssetBundle对象中的全部资源资源

       

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
            while(Caching.ready==false)
            {
                yield return null;//内存未准备好就再等一帧
            }
          // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
           string path1 = @"http://localhost/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
           // string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";
            WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            yield return WWW1;//等待加载完成
            if(string.IsNullOrEmpty(WWW1.error))//如果www1的错误为空
            {
                AssetBundle ab = WWW1.assetBundle;
                AssetBundleRequest request = ab.LoadAllAssetsAsync(); //异步加载所有资源
                 yield return request;//等待资源加载完成
                Object[]  go = request.allAssets;//异步加载所有资源
                foreach(var item in go)
                {
                    GameObject.Instantiate(item);//在场景中加载该物体
                }
            }
            else//如果www1的错误不为空
            {
                Debug.Log("加载出错");
            }
        }

                    2 UnityWebRequest类下载   UnityWebRequest request=UnityWebRequest.GetAssets(path)

                    同步加载AssetBundle对象中的资源的指定资源   

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
           
         // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
            string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
            UnityWebRequest request = UnityWebRequest.GetAssetBundle(path3);//从服务器中下载资源
            // WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            //yield return WWW1;//等待加载完成
            yield return request.SendWebRequest();//等待资源下载完毕
            if (request.isNetworkError || request.isHttpError)//判断是否有下载错误
            {
                Debug.Log("error");
            }
            else
            {
                AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
                AssetBundle ab1 = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//功能相同
                GameObject go = ab1.LoadAsset<GameObject>("sphere");//加载特定的资源到游戏中
                Instantiate(go);//将资源再场景中加载出来
    
            }

                     异步加载AssetBundle对象中的资源的指定资源   

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
           
         // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
            string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
            UnityWebRequest request = UnityWebRequest.GetAssetBundle(path3);//从服务器中下载资源
            // WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            //yield return WWW1;//等待加载完成
            yield return request.SendWebRequest();//等待资源下载完毕
            if (request.isNetworkError || request.isHttpError)//判断是否有下载错误
            {
                Debug.Log("error");
            }
            else
            {
                AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
                AssetBundle ab1 = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//功能相同
                AssetBundleRequest rquest1 = ab1.LoadAssetAsync<GameObject>("sphere");//异步加载特定的资源到游戏中
                GameObject go = rquest1.asset as GameObject;
                Instantiate(go);//将资源再场景中加载出来
    
            }

        同步加载AssetBundle对象中的全部资源资源

       

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
           
         // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
            string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
            UnityWebRequest request = UnityWebRequest.GetAssetBundle(path3);//从服务器中下载资源
            // WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            //yield return WWW1;//等待加载完成
            yield return request.SendWebRequest();//等待资源下载完毕
            if (request.isNetworkError || request.isHttpError)//判断是否有下载错误
            {
                Debug.Log("error");
            }
            else
            {
                AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
                AssetBundle ab1 = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//功能相同
                Object[] obj = ab.LoadAllAssets();//同步将所有资源加载出来
                foreach(var item in obj)
                {
                    Instantiate(item);//将资源再场景中加载出来
                }
            
    
            }

         异步加载AssetBundle对象中的全部资源

          

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
           
         // string path = @"file:///D:A计划A计划项目进阶AssetBundleAssetBundleStudyAssetBundlescube.unity";//本地资源资源的地址
            string path3 = @"http://localhost:5683/AssetBundles/perfabs/sphere.unity";//服务器资源的地址
            UnityWebRequest request = UnityWebRequest.GetAssetBundle(path3);//从服务器中下载资源
            // WWW WWW1 = WWW.LoadFromCacheOrDownload(path1,1);//从服务器加载资源
            //yield return WWW1;//等待加载完成
            yield return request.SendWebRequest();//等待资源下载完毕
            if (request.isNetworkError || request.isHttpError)//判断是否有下载错误
            {
                Debug.Log("error");
            }
            else
            {
                AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
                AssetBundle ab1 = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//功能相同
                AssetBundleRequest request1 = ab.LoadAllAssetsAsync();
                yield return request1;//等待资源加载完成
                Object[] obj = request1.allAssets;//异步将所有资源加载出来
                foreach(var item in obj)
                {
                    Instantiate(item);//将资源再场景中加载出来
                }
            
    
            }

                    

          3 使用从内中加载AssetBundle资源到游戏中

                                                                              

          AssetBundle  public  Functions  :1 T go=  ab. LoadAsset<T>(name) //同步加载AssetBundle对象中的资源的指定资源

                                                                 2 object[]  obj= ab.LoadAssets()//同步加载AssetBundle对象中的全部资源资源

                                                                 3  AssetBundleRequest request=ab.loadAssetAsync<T>(name);//异步加载AssetBundle对象中的资源的指定资源

                                                                 4  AssetBundleRequest request=ab.loadAssetsAsync<>;//异步加载AssetBundle对象中的所有资源

            同步加载AssetBundle对象中的资源的指定资源     

    void Start () {
            // StartCoroutine("CreateAssetBundle");//开启协程  
            
                AssetBundle ab = AssetBundle.LoadFromMemory( File.ReadAllBytes( "AssetBundles/perfabs/Sphere.unity"));//先把文件读到内存中(File.ReadAllBytes(path)),再把内存中的文件放入AssetBundle对象中
                GameObject go = ab.LoadAsset<GameObject>("Sphere");//把文件中Sphere物体从AssetBundle中加载出来
                GameObject.Instantiate(go);//在场景中创建该物体
            
        }

        同步加载AssetBundle对象中的全部资源资源

       

     AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes("AssetBundles/perfabs/Sphere.unity"));//先把文件读到内存中(File.ReadAllBytes(path)),再把内存中的文件放入AssetBundle对象中
            Object[] obj = ab.LoadAllAssets();
            foreach (var item in obj)
            {
                GameObject.Instantiate(item);//遍历循环,将文件中的所有物体都加载出来
            }

       异步加载AssetBundle对象中的资源的指定资源     

    void Start () {
             StartCoroutine("CreateAssetBundle");//开启协程         
        }
        IEnumerator CreateAssetBundle()
        {
            AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/perfabs/Sphere.unity"));
            yield return request;
            AssetBundle ab = request.assetBundle;
            AssetBundleRequest request1 = ab.LoadAssetAsync<GameObject>("Sphere");
            GameObject go = request1.asset as GameObject;
            GameObject.Instantiate(go);//在场景中创建该物体
              
        }

    异步加载AssetBundle对象中的全部资源资源

    void Start () {
            StartCoroutine("CreateAssetBundle");//开启协程 
        }
        IEnumerator CreateAssetBundle()
        {
            AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/perfabs/Sphere.unity"));
            yield return request;//等待从内存中加载文件
            AssetBundle ab = request.assetBundle;    //将资源转化成AssetBundle对象
            AssetBundleRequest request2 = ab.LoadAllAssetsAsync();//异步加载资源
            yield return request2;//等待资源异步加载完成
            Object[] obj = request2.allAssets;//将资源转成Object类型对象
            foreach (var item in obj)
            {
                GameObject.Instantiate(item);//遍历循环,将文件中的所有物体都加载出来
            }
          
    
        }

     5:资源的依存关系 

           一个AssetBundle资源物体使用了共享资源,则在他加载使用之前,要把他的共享资源加载到游戏中。

    6:关于文件的校验码

         CLC循环冗余校验码。MD5,SHA5校验码。

         区别:1 校验码的长度不同

                    2 MD5,SHA5叫哈希值或散列值

                    3安全性不同 SHA5>MD5>CLC

                    4效率不同 CLC>MD5>SHA5

                    5用途不同 CLC用于文件传输检验,MD4.SHA5用于安全检测

         

    7:AssetBundle打包的工具:  AssetBundle Browser Tools 将设置好AssetBundle属性的资源进行打包。

      Unity Manual -----AssetBundle -----AssetBundle Browser Tools---GitHub下载

       Configure:已经打包好的资源

       Build:打包资源  可以选择打包的平台和打包的路径等

    8:我们打包时将一些共享资源给单独打包的,在使用该资源前,要把他依存的共享资源先下载,然后再加载该资源

        方法:利用AssetBundles文件夹中AssetBundleManifest属性中存储的所有资源的依存的信息,获取指定的资源的依靠资源,将其加载到Unity中

                 string Path="AssetBundles/AssetBundles"//系统创建的AssetBundles文件的地址

                 AssetBundle ab=AssetBundle.LoadFromFile(Path)//将AssetBundles文件加载到Unity中

                AssetBundleManifest   manifest=AB.LoadAsset<AssetBundleManifest>("AssetBundleManifest")//获取AssetBundleS文件中的AssetBundleManifest属性

               string[]   name=manifest.GetAllDependeries("cube.unity")//获取Manifest属性中存储的Cube.Unity资源的依靠资源的信息

                foreach(var item in name)

               {

                   path1="AssetBundles"+item;//Cube所依靠的资源存储的相对路径

                   AssetBundle.LoadFromFile(path1);//将Cube所依靠的资源加载到Unity中

               }

        注:我们在利用AssetBundle存储资源时,系统会自动在你创建的AssetBundles文件夹下在创建一个AssetBundles文件夹,用于存储AssetBundles文件夹中存储的所有资源的信                息,比如资源的存放地址,资源之间的依存关系。

             代码:

     void Start()
        {
            //加载Cube所依存的资源
            string path = @"AssetBundlesAssetBundles";//系统自动生成的AssetBundles存储着所有资源的存储地址,和所有资源的依存关系。
            AssetBundle ab = AssetBundle.LoadFromFile(path);//获取到系统自动生成的AssetBundles里的所有文件夹
            AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//获取AssetBundle文件夹中AssetBundleManifest属性的内容
            string[] depends = manifest.GetAllDependencies("cube.unity");//获取AssetBundle文件夹中AssetBundleManifest属性中存储着Cube.unity(后缀不能少)资源所依存的资源名字
            foreach (var name in depends)
            {
                AssetBundle.LoadFromFile("AssetBundles/" + name);//依靠所获取到的资源名,将资源从本地加载到Unity中
            }
            //加载Cube对象
            string path1 = @"AssetBundlescube.unity";//Cube资源的地址
            AssetBundle ab1 = AssetBundle.LoadFromFile(path1);//加载Cube文件
            GameObject go = ab1.LoadAsset<GameObject>("cube");//获取Cube对象
            Instantiate(go);//在场景中创建出
    
        }

    9:StreamingAssets:文件夹中的资源在发布时不会进行任何处理(处理指压缩等),一般我们把游戏运行的必备资源放在这个文件夹中。                                                        

                                           

  • 相关阅读:
    大三寒假学习进度(3)
    大三寒假学习进度(2)
    大三寒假学习进度(1)
    Tensorflow深度学习(二)
    Tensorflow深度学习(一)
    了解使用Pyppeteer
    为什么我还可以继续使用python自动填问卷星?
    周总结(十四)
    docker常用命令总结
    周总结(十三)
  • 原文地址:https://www.cnblogs.com/zhangyang4674/p/10908137.html
Copyright © 2020-2023  润新知