非缓存机制:不会将请求到的资源写入到unity的缓存区
缓存机制:会将请求到的资源写入到unity的缓存区
区别:
非缓存机制每次都将向服务器请求资源
缓存机制:在使用资源时,将先访问unity的缓存区,如果缓存区中存在需要使用的资源,将直接获取使用,如果不存在,则向服务器请求资源
//using 的三个作用
导入命名空间
赋值(讲一个已有的命名空间赋值给另外一个命名空间)
自动释放(销毁)继承自IDisposable接口的类的实例对象
首先 创建以下文件夹
在TestScence new a Cube prefab,select cube —》set Assetbundle is assetbundle(仅支持小写(自动转变))
仅仅分辨率不同的时候,用第二个(后缀名)来区分
新版打包方式:可以打包,不能加载资源
在Editor创建脚本
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ExportGameObject : MonoBehaviour { [MenuItem("Test/BuildAssetbundle")]
static void BulidGamoObject(){
BuildPipeline.BuildAssetBundles("Assets/MyAssetBundles",BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets,BuildTarget.StandaloneWindows64); } }
Click Test-》BuildAssetbundle,在MyAssetBundles自动生成4个文件
旧版打包方式
New ExportScence ,new(cube),new ExPortAssetBundle.Cs in Editor
//打包Obj
[MenuItem("Build/ExportAssetBundle")]
static void ExportGameObject(){
//打开保存面板 参数1保存面板标题 2目录 3默认资源名称 4拓展名
string path = EditorUtility.SaveFilePanel ("Save GameObject", "", "New GameObject", "assetbundle");
if (path.Length != 0) {
//按照给定的类型来打包 获取在Project视图中选择的所有游戏对象Object[]selections=Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets); BuildPipeline.BuildAssetBundle (Selection.activeObject,selections,path,BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets,BuildTarget.StandaloneWindows64);}
AssetDatabase.Refresh ();//自动刷新unity编辑器}
// Object[] assets:指定所有asset,传入对象数组.解析时通过LoadAsset(“name”)得到
// pathName : 指定生成的AB包的存储路径
// BuildAssetBundleOptions:打包时候的一些特定属性选项。
// targetPlatform:适用平台。选择该AB应该使用哪些平台。
// CompleteAssets : 用于保证资源的完备性,默认开启;
// CollectDependencies:用于收集资源的依赖项,默认开启;
// DeterministicAssetBundle:用于为资源维护固定ID,默认开启;
// ForceRebuildAssetBundle:用于强制重打所有AssetBundle文件,新增;
// IgnoreTypeTreeChanges:判断AssetBundle更新时是否忽略TypeTree的变化
// AppendHashToAssetBundleName:用于将Hash值添加在AssetBundle文件名之后,开启这个选项可以直接通过文件名来判断哪些Bundle的内容进行了更新(4.x下普遍需要通过比较二进制等方法来判断,但在某些情况下即使内容不变重新打包,Bundle的二进制也会变化)。
// ChunkBasedCompression:用于使用LZ4格式进行压缩,5.3新增
打包游戏场景
[MenuItem("Build/ExportScene")]
static void ExportScene(){
Caching.CleanCache (); //强制清空缓存
//打开保存面板
string path = EditorUtility.SaveFilePanel ("Save Scene", "", "New Scene", "unity3d");
if (path.Length != 0) {
string[] scenes={"Assets/Scene/testTwo.unity"} ; //指定需要打包成Assetbundle的资源路径
//打包AssetBundle 参数:游戏对象的数组 打包后的保存路径 打包后的目标平台 打包方式BuildPipeline.BuildStreamedSceneAssetBundle (scenes,path,BuildTarget.StandaloneWindows64,BuildOptions.BuildAdditionalStreamedScenes);}
AssetDatabase.Refresh (); //自动刷新unity编辑器 }
打包对象时,选中预设体,Build (加assetbundle后缀名都行)保存到MyAssetbundleItems文件夹,保存,刷新就出来了
打包游戏场景 Build (加unity3d后缀名)保存到MyAssetbundleItems文件夹,保存,刷新就出来了
New Test.cs in Scripts folder
using UnityEngine;
using System.Collections;
using System;
public class Test : MonoBehaviour {
private string GoURL="file:////Users/Desktop/Assets/MyAssetBundlesItems/New GameObject.assetbundle";
private string SceneUrl="file:////Users/Desktop/Assets/MyAssetBundlesItems/New Scene.unity3d";
public string CubePre;
private int Version=1;
void Start () {
// StartCoroutine (LoadGameObject());
// StartCoroutine (LoadGameObjectByCache()); StartCoroutine (LoadSceneByCache()); }
/* LoadFromCacheOrDownload():该方法包含两个参数:第一个参数为请求链接,第二个参数为版本号 如果服务器有资源更新,版本号将会发生改变 版本号改变将会导致程序重新向服务器请求最新资源 如果版本号不发生改变 程序将使用之前缓存的资源,不会向服务器发送请求 */
//缓存机制
IEnumerator LoadGameObjectByCache(){
using (WWW www = WWW.LoadFromCacheOrDownload (GoURL, Version)) {
yield return www;
if (www.error != null) {
Debug.Log ("Error " + www.error);
} else {
AssetBundle bundle = www.assetBundle;
Instantiate (bundle.mainAsset);
bundle.Unload (false);} } }
// 非缓存机制
IEnumerator LoadGameObject(){
//111手动回收
// WWW www = new WWW (GoURL);
// yield return www;//等待请求完成
// if (www.error != null) {
// Debug.Log ("ErrorInfo " + www.error);
// throw new Exception ("ErrorInfo " + www.error);
// } else {
// AssetBundle bundle = www.assetBundle;请求获取资源
// Instantiate (bundle.mainAsset);组资源进行动态创建
// bundle.Unload (false);//卸载资源
// }
// www.Dispose ();手动回收,销毁www对象所占内存
//222自动回收
using(WWW www = new WWW (GoURL)){
yield return www;
if (www.error != null) {
Debug.Log ("ErrorInfo " + www.error);
} else {
AssetBundle bundle = www.assetBundle;
if (CubePre == string.Empty) {
Instantiate (bundle.mainAsset);
} else {
Instantiate (bundle.LoadAsset(CubePre));
}
bundle.Unload (false); } } }
// 如果运行的Game里天空盒不是正常天空盒;则在打包的场景里把摄像机的ClearFlags该为DOn'tclear 然后重新打包(看文件里的路径是否正确) IEnumerator LoadSceneByCache(){
using (WWW www = WWW.LoadFromCacheOrDownload (SceneUrl, Version)) {
yield return www;
if (www.error != null) {
Debug.Log ("Error " + www.error);
} else {
AssetBundle bundle = www.assetBundle;
Application.LoadLevel ("testTwo"); } } } }