U3D Resources AssetBundle资源打包
Resources
在Unity中可以使用www类加载远程文件或本地文件,或是在脚本中定义字段或数组从外部拖入.
在Unity中提供了Resources类读取资源
要通过Resources类读取的文件必须将要读取的文件放到Assets下创建的Resources(名字唯一)文件夹下.
在脚本中通过Resources类的方法可直接加载Resources文件夹中的数据

1.通过名称获取资源
Resources.Load("文件名");
2.通过名称获取指定类型的资源(泛型方法)
Resources.Load<Texture>("文件名");
3.通过名称获取指定类型的资源
Resources.Load("文件名",typeof(Texture)) as Texture;
//通过代码直接创建游戏对象
//CreatePrimitive,创建一个带有基本网格渲染器和相应放入碰撞器的游戏对象
GameObject cylinder = GameObject.CreatePrimitive (PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3 (2f, 0, 0);
cylinder.GetComponent<MeshRenderer> ().material.color =
new Color (Random.value, Random.value, Random.value, Random.value);
注:通过此方法只能 创建几种系统指定的游戏物体(Cube,sphere...).

AssetBundle类资源打包
AssetBundle是Unity引擎提供的一种压缩资源文件,文件的扩展名通常为unity3d或assetbundle
可以使用AssetBundle打包多种类型的资源


Editor打包AssetBundle
早Assets下创建Editor文件夹,在里面创建一个用作打包的类



注:如果资源存在依赖关系,打包时需要连同依赖资源一起打包
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
/// <summary>
/// 编辑器类,用来实现AssetBundle打包
/// </summary>
public class AssetBundle
{
//MenuItem:可以在Unity编辑器的菜单栏中出现一个后面()中的选项,下面方法为编辑器点击响应的方法
//因为编辑器类是静态类,所以必须是静态方法,在方法中实现对选中的资源进行打包
[MenuItem ("AssetBundle/Build")]
static void BuildAssetBundle ()
{
BuildPipeline.BuildAssetBundles (Application.dataPath + "/AssetBundle",
BuildAssetBundleOptions.None,BuildTarget.StandaloneOSXIntel);
}
}
创建AssetBundle解压(加载)资源

脚本如下:
//解压资源包的协程方法
IEnumerator LoadAsset ()
{
//配置打包后的资源所在文件夹
string assetPath = "file://" + Application.dataPath + "/AssetBundle/";
//总的Manifest文件名
string manifestName = "AssetBundle";
//要加载的目标资源的名字
string targetAssetName = "Cube";
//LoadFromCacheOrDownload:从缓存中加载一个带有特定版本号的资源包
//如果该资源包没有缓存,就会通过路径加载,他将自动下载并存储在高速缓存中,一遍下次从本地读取
WWW wwwManifest = WWW.LoadFromCacheOrDownload (assetPath + manifestName, 0);
//等待资源下载完成
yield return wwwManifest;
//判断如果下载没有出错
if (wwwManifest.error == null) {
Debug.Log ("下载没问题");
//得到AssetBundle的包
UnityEngine.AssetBundle maniFestBundle = wwwManifest.assetBundle;
//通过清单得到manifest文件对于资源的说明,以及各个资源的依赖关系
AssetBundleManifest manifest = (AssetBundleManifest)maniFestBundle.LoadAsset ("AssetBundleManifest");
//根据总的资源清单获取目标资源
string[] dps = manifest.GetAllDependencies (targetAssetName);
//保存所有的依赖资源
UnityEngine.AssetBundle[] abs = new UnityEngine.AssetBundle[dps.Length];
//遍历所有资源
for (int i = 0; i < dps.Length; i++) {
//找到各个依赖资源所在的位置
string durl = assetPath + dps [i];
//根据路径下载资源
WWW dwww = WWW.LoadFromCacheOrDownload (durl, 0);
//等待下载完成
yield return dwww;
//将下载的目标资源从头到尾存放到资源集合中
abs [i] = dwww.assetBundle;
}
//加载目标文件
WWW cubewww = WWW.LoadFromCacheOrDownload (assetPath + targetAssetName, 0);
//等待下载完成
yield return cubewww;
if (cubewww.error == null) {
//得到Cube的资源列表
UnityEngine.AssetBundle cubeBundle = cubewww.assetBundle;
//通过资源包获取相对应的资源
GameObject cube = cubeBundle.LoadAsset (targetAssetName) as GameObject;
//创建对象
Instantiate (cube, Vector3.zero, Quaternion.identity);
}
}
}
什么是AssetBundle,可以包含哪些文件,它们如何在开发过程中使用?
1、AssetBundles 是有 Unity 编辑器在编辑环境中创建的一些列的文件,这些文件可以被用在项目的运行环境中(run-time)。
2、AssetBundles 可以包括的资源文件有模型文件(models)、材质(materials)、纹理(textures)和场景(scenes)。
3、具体来说,一个 AssetBundle 就是把一系列的资源文件或者场景文 件以某种方式紧密保存的一个文件。这个 AssetBundle 文件可以被单独加载到可执行的应用程序中。
4、AssetBundles 可以由被 Unity 构建的游戏或者应用按需加载使用。这允许对像模型、纹理、音频、甚至是整个的游戏场景这样的资源进行流式加载和异步加载。
5、AssetBundles 可以预缓存和存储在本地,这样在运行时就可以立即加载它们。但是 AssetBundles 技术的主要的目的是在需要的时候能够从远端的服务器上按需请求特定的资源,并加载到游戏中。
描述AssetBundle 和 Reousrces 的区别
1、Resources是动态内部调用,Resources在编辑环境下是project窗口的一个文件夹,调用里面的资源,可以用Resources类,比如Resources.Load,打包后这个文件夹是不存在的,会统一生成assets资源。
2、AssetBundle 是外部调用,要用AssetBundle 首先要先把资源打包为.assetbundle文件,再动态的去加载这个文件,本地或者网络服务器都可以使用。

浙公网安备 33010602011771号