首先要说的是,我们的project中有2个脚本。各自是:
Build(编辑器类脚本。无需挂载到不论什么物体)。可是必需要把Build脚本放到Editor目录中
Load脚本,挂载到摄像机上<pre name="code" class="csharp">using UnityEngine;
using System.Collections;
using UnityEditor; public class Build : MonoBehaviour
{
// 编辑器类
[MenuItem("Build/BuildAsset")]
// 打包单个资源的方法
static void BuildAsset ()
{
// 将物体打包到/AssetsBundles路径下同一时候命名为Cube1.assetbundle
string targetPath = Application.dataPath + "/AssetsBundles/Cube1.assetbundle";
// BuildPipeline 打包用 Selection (Selection.activeObject 在编辑器中选中的对象)
// 第二个參数null,是打包多个资源的时候用的。多个资源就是数组。单个资源的时候这个參数不用写
// 第三个參数是路径。你想把这个资源放到什么位置就写什么,而且还要命名为.assetbundl格式
BuildPipeline.BuildAssetBundle (Selection.activeObject, null, targetPath);
// 刷新资源,直接能够在unity中看到刚才打包的东西
AssetDatabase.Refresh();
} // -------------------------------------------------------------------------------- [MenuItem("Build/BuildMultiAsset")]
// 打包多个资源的时候使用这种方法
static void BuildMultiAsset()
{
// 将物体打包到/AssetsBundles路径下同一时候命名为Cubes.assetbundle
string target = Application.dataPath + "/AssetsBundles/Cubes.assetbundle";
// 由于打包多个游戏对象。所以是一个数组,typr(Object)表示,仅仅要选择的物体是Object类型就能够
// 而且选择的模式是深度资源搜索(详细学名究竟叫啥我也忘了。。 。 )。
// 意思是仅仅要你选择的object对象以下假设还有关联的东西就都会一起打包(unity导出场景的时候应该能够感受到)
Object[] cubes = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
// 打包開始,由于是一堆资源。所以主资源为null,剩下两个參数意思同上
BuildPipeline.BuildAssetBundle(null,cubes,target);
// 刷新资源,直接能够在unity中看到刚才打包的东西
AssetDatabase.Refresh();
} // -------------------------------------------------------------------------------- [MenuItem("Build/BuildScene")]
// 打包场景的方法
static void BuildScene()
{
// 场景名字,为什么是数组呢(由于參数里要求这个參数是数组类型。。 。。 )
string [] sceneName = new string[]{Application.dataPath + "/scene1.unity"};
// 将物体打包到/AssetsBundles路径下同一时候命名为Scene1.assetbundle
string targetPath = Application.dataPath + "/AssetsBundles/Scene1.assetbundle";
// 注意打包的方法名已经变成流类型了,后面的最后一个參数表示你要在上面平台执行
// iphone平台就是用BuildTarget.iPhone这个參数
// 安卓平台就是用BuildTarget.Android这个參数
// 相似參数能够自己查看。。。
BuildPipeline.BuildStreamedSceneAssetBundle(sceneName,targetPath,BuildTarget.WebPlayer);
// 刷新资源。直接能够在unity中看到刚才打包的东西
AssetDatabase.Refresh();
}
// --------------------------------------------------------------------------------
[MenuItem("Build/BuildDependence")]
static void BuildDependence()
{
// 对一些公共參数的定义 //
// 定义路径目录是Application.dataPath读取后的路径 加上 "/AssetBundles"
string path = Application.dataPath + "/AssetsBundles";
// 定义资源包的依赖关系选项
BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets; // --------------------------------------------------------------------- // Push和Pop是成对出现的。他们的作用是维持一个依赖关系
// 我们当前的资源包关系是这种:
/*
Tex1.assetbundle资源包(Tex1的包里有以下2个资源包)
c1.assetbundle资源包(c1和c2是平级关系)
c2.assetbundle资源包
*/
//依赖资源压栈
BuildPipeline.PushAssetDependencies(); // 替代之前的selection的手动选择方式
Object tex = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Tex1.jpg"); // 全部兴许资源将共享这一资源包中的内容,可是你要保证的是,在使用c1和c2这种内层依赖包时
// 你已经载入了最外层资源包Tex1
BuildPipeline.BuildAssetBundle(tex, null,path + "/Tex1.assetbundle", buildOp); // 这里使用一对Push和Pop是将我们接下来载入的c1资源包和tex依赖起来
BuildPipeline.PushAssetDependencies();
Object c1 = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Cube1.prefab");
BuildPipeline.BuildAssetBundle(c1, null, path + "/c1.assetbundle", buildOp);
BuildPipeline.PopAssetDependencies(); // 同上
BuildPipeline.PushAssetDependencies();
Object c2 = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Cube2.prefab");
BuildPipeline.BuildAssetBundle(c2, null,path + "/c2.assetbundle", buildOp);
BuildPipeline.PopAssetDependencies(); // 依赖资源出栈
BuildPipeline.PopAssetDependencies();
AssetDatabase.Refresh();
} }
using UnityEngine;
using System.Collections; public class Load : MonoBehaviour {
// 全部公共的路径名开头都是这个目录,所以单列出来,简化后面代码
public static string loadPath = "file://" + Application.dataPath +"/AssetsBundles"; // 画一个button
void OnGUI()
{
if(GUILayout.Button("LoadTex"))
{
StartCoroutine(LoadTex(loadPath));
}
if (GUILayout.Button("LoadCube")) {
// 由于以下的方法都是协同程序,所以须要手动开启协同程序
StartCoroutine(LoadCube(loadPath));
}
if (GUILayout.Button("LoadVersion")) {
StartCoroutine(LoadVersion(loadPath));
}
if (GUILayout.Button("LoadCubes")) {
StartCoroutine(LoadCubes(loadPath));
}
if (GUILayout.Button("LoadScene")) {
StartCoroutine(LoadScene(loadPath));
}
} // 协同方法,载入1个cube的方法
IEnumerator LoadCube (string path)
{
// 下载一个已经打包好的assetbundle(打包须要在Build脚本中实现,在project里手动打包)
WWW www = new WWW(path + "/Cube1.assetbundle");
// 返回下载的assetbundle包
yield return www;
// 实例化一个cube,游戏物体使用assetbundle中的主资源(由于是单独打包,所以仅仅有一个主资源),后面是实例化物体的位置和角度信息
Instantiate(www.assetBundle.mainAsset,new Vector3(Random.Range(0,9),Random.Range(0,9),Random.Range(0,9)),Quaternion.identity);
// 实例化成功后输出资源名字
print(www.assetBundle.mainAsset);
// 卸载刚才已经下载的资源在内存中(就是在内存中删除)
www.assetBundle.Unload(false);
} // 协同方法,载入多个cube的方法
IEnumerator LoadCubes(string path)
{
// 下载一个已经打包好的包括多个游戏对象的资源包
WWW www = new WWW(path + "/Cubes.assetbundle");
yield return www;
// 下载资源包中的当中一个名字叫Cube1的物体(这个名字一定是在刚才下载的那个包里有的)
Instantiate(www.assetBundle.Load("Cube1"),Vector3.up,Quaternion.identity);
// 下载资源包中的当中一个名字叫Cube2的物体(这个名字一定是在刚才下载的那个包里有的)
Instantiate(www.assetBundle.Load("Cube2"),Vector3.up,Quaternion.identity);
} // 协同方法,载入一个场景的方法
IEnumerator LoadScene(string path)
{
// 下载已经打包好的场景
WWW www = new WWW(path + "/scene1.assetbundle");
yield return www;
// 获取到包括场景的assetbundle资源包
AssetBundle bundle = www.assetBundle;
// 载入的是资源包中的scene1场景,假设没有上面的这行代码。就无法载入这个场景
Application.LoadLevel("scene1");
} IEnumerator LoadVersion(string path)
{
// 依据版本载入assetbundle
WWW www = WWW.LoadFromCacheOrDownload(path + "/Cube1.assetbundle",1);
yield return www;
if (www.error != null) {
print(www.error);
} else {
Instantiate(www.assetBundle.mainAsset);
}
} IEnumerator LoadTex(string path)
{
WWW www = new WWW(path + "/Tex1.assetbundle");
WWW wwwc1 = new WWW(path + "/c1.assetbundle");
yield return www;
print(www.assetBundle.mainAsset);
Instantiate(wwwc1.assetBundle.mainAsset); }
}


【Unity】Unity中资源动态载入的两种方式之AssetsBundle的更多相关文章

  1. Eclipse中构建maven项目的两种方式

    Eclipse中构建maven项目的两种方式 方式一: 1.构建maven项目 1.1 新建meven项目,可在Other中找到maven文件夹 1.2 进入maven项目后,点击next 1.3 在 ...

  2. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...

  3. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  4. HTML中设置背景图的两种方式

    HTML中设置背景图的两种方式 1.background    background:url(images/search.png) no-repeat top; 2.background-image ...

  5. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  6. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  7. .Net 中读写Oracle数据库常用两种方式

    .net中连接Oracle 的两种方式:OracleClient,OleDb转载 2015年04月24日 00:00:24 10820.Net 中读写Oracle数据库常用两种方式:OracleCli ...

  8. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  9. 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制

    通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...

随机推荐

  1. Struts2 中#、@、%和$符号的用途

    一.#符号的用途一般有三种. “#”主要有三种用途: 1. 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():下表有几个ActionContext中 ...

  2. STL 源代码剖析 算法 stl_algo.h -- next_permutation

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...

  3. cocos2d-x 3.1.1 学习笔记[15] Shader 著色器

    首先须要两个文件 gray.fsh varying vec4 v_fragmentColor; varying vec2 v_texCoord; void main() { vec4 v_orColo ...

  4. Some Web API Url Samples

    URI                               Verb     Description                                               ...

  5. 【MVC5】日期选择控件DatePicker

    项目中使用了Bootstrap,日期控件就选择了依赖于bootstrap的DatePicker. 在App_Start\BundleConfig.cs中引用css和js文件: bundles.Add( ...

  6. POJ 2976 Dropping tests (最大化平均值)

    题目链接:click here~~ [题目大意]给你n个分数的值,要求最小不选k个,使得最后分数相加结果平均值最大 [解题思路]:最大化平均值:參见:click here~~ 代码: #include ...

  7. Docker学习笔记之一,搭建一个JAVA Tomcat运行环境(转)

    前言 Docker旨在提供一种应用程序的自动化部署解决方案,在 Linux 系统上迅速创建一个容器(轻量级虚拟机)并部署和运行应用程序,并通过配置文件可以轻松实现应用程序的自动化安装.部署和升级,非常 ...

  8. 使用Parallel实现简单的并行操作

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. js操作时间 加法 减法 计算 格式化时间

    Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...

  10. Excel去除单元格中的汉字

    Alt+F11,插入,模块 Function RemoveChinese(rng As Range) s = Len(rng.Text) For i = 1 To s txt = StrConv(Mi ...