AssetBundle打包和加载

 好吧!废话不多讲,直接上图上操作哈!
  1.首先还是新建一个unity3d项目,导入需要资源文件,我这边导入一个asset store 模型插件 unity-chan资源。
  2.第一步,我们先创建打包AssetBundle 的环境的,项目需要一个内置的文件夹Editor (这是必须操作,这是为了可以在项目编辑器中打包AssetBundle资源),并且附上AssetBundleEditor.cs组件类。  需要注意的是 :不同的平台参数是不一样的,IOS平台上Assetbundle打包时需要使用 BuildTarget.iPhone 参数。
 

 

  AssetBundleEditor.cs 代码如下:

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4. using System.IO;  
  5.    
  6. public class AssetBundleEditor : Editor   
  7. {  
  8.    
  9.         public static string sourcePath = Application.dataPath + "/Resource";  
  10.         private static string AssetBundlesOutputPath = Application.dataPath + "/StreamingAssets";  
  11.    
  12.         [MenuItem("AssetBundles/BuildAssetBundle")]  
  13.         public static void BuildAssetBundle()  
  14.         {  
  15.                 string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(BuildTarget.iOS));  
  16.                 if (!Directory.Exists(outputPath))  
  17.                 {  
  18.                         Directory.CreateDirectory(outputPath);  
  19.                 }  
  20.    
  21.                 //根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包  
  22.                 BuildPipeline.BuildAssetBundles("Assets/StreamingAssets/IOS",BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS);  
  23.    
  24.                 AssetDatabase.Refresh();  
  25.    
  26.                 Debug.Log("打包完成");  
  27.    
  28.         }  
  29. }  
  30.    
  31. public class Platform  
  32. {  
  33.         public static string GetPlatformFolder(BuildTarget target)  
  34.         {  
  35.                 switch (target)  
  36.                 {  
  37.                 case BuildTarget.Android:  
  38.                         return "Android";  
  39.                 case BuildTarget.iOS:  
  40.                         return "IOS";  
  41.                 case BuildTarget.StandaloneWindows:  
  42.                 case BuildTarget.StandaloneWindows64:  
  43.                         return "Windows";  
  44.                 case BuildTarget.StandaloneOSXIntel:  
  45.                 case BuildTarget.StandaloneOSXIntel64:  
  46.                 case BuildTarget.StandaloneOSXUniversal:  
  47.                         return "OSX";  
  48.                 default:  
  49.                         return null;  
  50.                 }  
  51.         }  
  52. }  




  3.第二步,我们把打包好的AssetBundle资源解包出来,现在在场景中新建一个LoadAssetBundle的对象,实现加载asset bundle资源的脚本。
  LoadAssetBundle.cs 代码如下: 

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.    
  5. public class LoadAssetBundle : MonoBehaviour  
  6. {  
  7.    
  8.         public  string PathURL;  
  9.         public GameObject CurrentObj;  
  10.    
  11.         void Start()  
  12.         {  
  13.                 #if UNITY_IPHONE    
  14.                 PathURL = "file://"+Application.dataPath + "/Raw/IOS";    
  15.                 #elif UNITY_STANDALONE_WIN || UNITY_EDITOR    
  16.                 PathURL = "file://"+Application.streamingAssetsPath+"/IOS";  
  17.                 #endif    
  18.    
  19.         }  
  20.    
  21.         void OnGUI()  
  22.         {  
  23.                 if(GUI.Button(new Rect(100,100,100,100),"加载美少女"))  
  24.                 {  
  25.                         StartCoroutine (GetAssetBundleObj("girl_001",PathURL));  
  26.                 }  
  27.         }  
  28.    
  29.         public  IEnumerator GetAssetBundleObj(string objName,string path="")  
  30.         {  
  31.                 string filePath = System.IO.Path.Combine (path,objName);  
  32.    
  33.                 WWW w = new WWW(filePath);         //利用www类加载  
  34.                 yield return w;  
  35.                 AssetBundle curBundleObj = w.assetBundle;     //获得AssetBundle  
  36.    
  37.                 AssetBundleRequest obj = curBundleObj.LoadAssetAsync(objName, typeof(GameObject));    //异步加载GameObject类型  
  38.                 yield return obj;  
  39.                 CurrentObj = Instantiate(obj.asset) as GameObject;  
  40.    
  41.                 yield return null;  
  42.                 curBundleObj.Unload(false);     //卸载所有包含在bundle中的对象,已经加载的才会卸载  
  43.                 w.Dispose();  
  44.         }  
  45.    
  46. }  



   
  4.其实从运行项目的时候,我们会发现模型的加载不正常,因为它丢失了材质,而我们找到的解决的方法是在Edit->Project Setting->Graphics 设置中的Always Include Shaders 中增加模型的Shader,Shader配置好以后,记得重新打包模型。
 

 

 


5.好吧,接下来我们Build一个版本在真机上,这个涉及到 IOS 开发部署流程,有兴趣的同学可以去研究一下,然后,我在这边直接操作啦!
  
5.0 首先BuildSetting 切换平台到IOS;
 
  
5.1 直接在真机上的效果。
 
最后,我们来看一下www访问方式:
(1).非缓存方式:
WWW w=new WWW(url:string);
通过创建一个WWW实例来对AssetBundle文件下载,下载后的AssetBundle文件将不会进入Unity的缓存区。
   

(2)缓存方式:

WWW w=WWW.LoadFromCacheOrDownload(url:string,version:int);


下载后的AssetBundle会自动被保存到Unity引擎的缓存区内,该方法是Unity推荐的AssetBundle下载方式。下载AssetBundle的时候,该接口会先在本地缓存中查找该文件,看其之前是否被下载过,如果下载过,则直接从缓存中加载,如果没有,则从服务器尽享下载。这样做的好处是可以节省AssetBundle文件的下载时间,从而提高游戏资源的载入速度(还可以节省下载流量)。
  
        注意:但是WWW.LoadFromCacheOrDownload(url:string,version:int)在测试情况下你可能会频繁的打包生成Assetbundle,如果忘记改版本号的话可能会读取之前的缓存,可能就会看不到新的效果,所以建议在bunild Assetbundle的时候强制清空一下缓存。
      Caching.CleanCache(); 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AssetBundle 引用计数是指在使用 AssetBundle 打包资源的时候,记录每个 AssetBundle 被使用的次数,以便在使用完后及时释放资源,从而避免内存泄漏。在使用 AssetBundle 加载资源时,会对加载的 AssetBundle 进行引用计数的增加,使用完后再进行引用计数的减少,当引用计数为 0 时,就可以释放该 AssetBundle 的资源。 AssetBundle打包加载一般分为以下几个步骤: 1. 打包资源文件:使用 Unity Editor 自带的 AssetBundle 打包工具,将需要打包的资源文件进行打包,生成 AssetBundle 文件。 2. 加载 AssetBundle 文件:在游戏运行时,使用 Unity 提供的 AssetBundle.LoadFromFile 或 AssetBundle.LoadFromMemory 函数来加载 AssetBundle 文件。 3. 加载资源文件:使用加载的 AssetBundle,使用 AssetBundle.LoadAsset 或 AssetBundle.LoadAssetAsync 函数加载需要使用的资源文件。 4. 使用完成后,释放资源:使用 AssetBundle.Unload(false) 函数来释放 AssetBundle 中的资源,同时进行引用计数的减少。如果不再需要该 AssetBundle 中的任何资源,可以使用 AssetBundle.Unload(true) 函数来彻底释放该 AssetBundle,包括清除 AssetBundle 的缓存。 使用 AssetBundle 打包加载资源可以有效地减少应用程序的内存占用,提高应用程序的性能。同时,在使用 AssetBundle 的时候,需要注意避免重复加载同一个 AssetBundle,以及及时释放不再使用的 AssetBundle 资源,避免内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值