Unity5.3更新了assetbundle的打包和加载api,下面简单介绍使用方法及示例代码。

  在Unity中选中一个prefab查看Inspector窗口,有两个位置可以进行assetbundle的标记。

  第一个为assetBundleName,如果这里不是None,则这个资源会记录到AssetDatabase里,使用BuildAssetBundles打包时,会自动将AssetBundleName一致的资源打到一个包中。

  第二个为Variant,可用于区分不同分辨率的资源。如果在abName=S的情况下,variant有None也有HD,打ab包时会报错,如果variant有指定,则同一abName的资源的variant不能为None。

     

// 此api用于打包所有标记了AssetBundleName的资源

public static AssetBundleManifest BuildAssetBundles(

  string outputPath,

  BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None,

  BuildTarget targetPlatform = BuildTarget.WebPlayer

);

// 此api用于打包指定的资源

public static AssetBundleManifestBuildAssetBundles(

  string outputPath,

  AssetBundleBuild[] builds,

  BuildAssetBundleOptionsassetBundleOptions = BuildAssetBundleOptions.None,

  BuildTargettargetPlatform = BuildTarget.WebPlayer

);

  其中,AssetBundleBuild数组参数,用于指定资源,AssetBundleBuild有三个属性,分别指定assetBundleName,variant,资源路径等。

 
 

  以下为打包及加载部分代码:大部分参考了雨凇MOMO的文章《UGUI研究院之全面理解图集与使用(三)》,做了部分修改。

 #define USE_ASSETBUNDLE

 using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEditor.VersionControl; public class UIMain : MonoBehaviour{ AssetBundle assetbundle = null;
void Start ()
{
CreatImage(loadSprite("flag_blue"));
CreatImage(loadSprite("flag_yellow"));
} private void CreatImage(GameObject gobj ){
Sprite sprite = gobj.GetComponent<SpriteRenderer>().sprite as Sprite;
GameObject go = new GameObject(sprite.name);
go.layer = LayerMask.NameToLayer("UI");
go.transform.parent = transform;
go.transform.localScale= Vector3.one;
Image image = go.AddComponent<Image>();
image.sprite = sprite;
image.SetNativeSize();
} private GameObject loadSprite(string spriteName){
#if USE_ASSETBUNDLE
if(assetbundle == null)
assetbundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath +"/flagbundle");
return assetbundle.LoadAsset(spriteName) as GameObject;
#else
return Resources.Load<GameObject>("Sprite/" + spriteName);
#endif
} }

UIMain

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using UnityEditor.VersionControl; public class AltasMaker : MonoBehaviour { [MenuItem ("MyMenu/AtlasMaker")]
static private void MakeAtlas()
{
string spriteDir = Application.dataPath + "/Resources/Sprite";
Debug.Log("spriteDir : " + spriteDir);
if(!Directory.Exists(spriteDir)){
Directory.CreateDirectory(spriteDir);
} DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath + "/Atlas");
foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories)) {
string allPath = pngFile.FullName;
Debug.Log("allPath1 : " + allPath);
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
Debug.Log("assetPath : " + assetPath);
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
GameObject go = new GameObject(sprite.name);
go.AddComponent<SpriteRenderer>().sprite = sprite;
allPath = spriteDir+ "/" +sprite.name+ ".prefab";
Debug.Log("allPath2 : " + allPath);
string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));
PrefabUtility.CreatePrefab(prefabPath, go);
GameObject.DestroyImmediate(go);
}
}
} [MenuItem ("MyMenu/Build Assetbundle")]
static private void BuildAssetBundle()
{
string outputdir = Application.dataPath + "/StreamingAssets"; if(!Directory.Exists(outputdir))
{
Directory.CreateDirectory(outputdir);
} DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath + "/Atlas");
foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
List<Sprite> assets = new List<Sprite>(); foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories))
{
string allPath = pngFile.FullName;
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
assets.Add(AssetDatabase.LoadAssetAtPath<Sprite>(assetPath));
} AssetBundleBuild[] buildMap = new AssetBundleBuild[]; buildMap[].assetBundleName = dirInfo.Name + "bundle"; // 想定义的任何名称
string[] spriteNames = new string[assets.Count];
for(int i = ; i < assets.Count; i++)
{
spriteNames[i] = "Assets/Atlas/" + dirInfo.Name + "/" + assets[i].name + ".png"; // 注意路径写全
}
buildMap[].assetNames = spriteNames;
buildMap[].assetBundleVariant = null; BuildPipeline.BuildAssetBundles(
outputdir, // output path
buildMap, // build bundles info
BuildAssetBundleOptions.UncompressedAssetBundle, // options
GetBuildTarget()); // build target
}
} [MenuItem ("MyMenu/Build All AssetBundles")]
static private void BuildAllAssetBundles()
{
BuildPipeline.BuildAssetBundles(
Application.dataPath + "/StreamingAssets", // output path
BuildAssetBundleOptions.ChunkBasedCompression, // options
GetBuildTarget());
} static private BuildTarget GetBuildTarget()
{
BuildTarget target = BuildTarget.WebPlayer;
#if UNITY_STANDALONE
target = BuildTarget.StandaloneOSXIntel64;
#elif UNITY_IPHONE
target = BuildTarget.iPhone;
#elif UNITY_ANDROID
target = BuildTarget.Android;
#endif
return target;
}
}

AltasMaker

  注:1 经过验证,同一个资源如果没有改变,不会被重新build

    2 如果缓存文件不刷新,强制调用Caching.CleanCache()。

  assetbundle有三种压缩格式:默认为LZMA模式(最小),还有LZ4和不压缩(最大)。

  不同压缩格式相关优缺点及使用详见下文:Unity5.x新的AssetBundle机制02——压缩

其他参考:

UGUI研究院之全面理解图集与使用(三)

【厚积薄发】揭开Unity AssetBundle庐山真面目(二)

Unity3D 5.3 新版AssetBundle使用方案及策略

AssetDatabase

[Unity] unity5.3 assetbundle打包及加载的更多相关文章

  1. Unity5 AssetBundle 打包以及加载

    using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; us ...

  2. Unity3d 5.x AssetBundle打包与加载

    1.AssetBundle打包 unity 5.x版本AssetBundle打包,只需要设置好AssetBundle的名称后,unity会自动将其打包,无需处理其他,唯独需要做的是设置好个AssetB ...

  3. unity中ScriptableObject在assetbundle中的加载

    转载请标明出处:http://www.cnblogs.com/zblade/ 以前都是写一些个人的调研博客,从今天开始,也写一些个人在开发中遇到的一些可以分享的趟坑博客,为后续的开发人员提供一些绵薄之 ...

  4. AssetBundle资源打包与加载

    AssetBundle资源打包  1.AssetLabels资源标签 文件名:资源打包成AssetBundle后的文件名,类似于压缩包的名字 后缀:自定义 文件名和后缀名都是小写格式(大写会自动转为小 ...

  5. Assetbundle管理与加载

    最近在做项目优化的时候发现公司的项目用的还是老式的WWW去加载assetbundle资源的形式,而且是通过在两个Update里面分开加载AB和Asset的,这样虽然避免了协程的的使用,但是把一件事分开 ...

  6. Unity开发实战探讨-资源的加载释放最佳策略简要心得

    Unity开发实战探讨-资源的加载释放最佳策略简要心得 看过我另外一篇关于Unity资源释放随笔<Unity开发实战探讨-资源的加载释放最佳策略>如果觉得略微复杂,那么下面是一些比较简要的 ...

  7. Unity开发实战探讨-资源的加载释放最佳策略

    注:本文中用到的大部分术语和函数都是Unity中比较基本的概念,所以本文只是直接引用,不再详细解释各种概念的具体内容,若要深入了解,请查阅相关资料. Unity的资源陷阱 游戏资源的加载和释放导致的内 ...

  8. Assetbundle创建与加载

    [Assetbundle创建与加载] Unity有两种动态加载机制:一种是Resource.Load.一种是AssetBundle.Assetbundle是Unity Pro提供的功能,它可以把多个游 ...

  9. Unity3D AssetBundle的打包与加载

    在Unity项目开发过程中,当要做热更新时常常使用一个叫做AssetBundle的东西,这里做一点个人的学习记录 步骤1: 设置打包标签:具体步骤----进入Unity,选择某一资源然后看右下角,在那 ...

随机推荐

  1. c++课程学习(未完待续)

    关于c++课程学习 按照计划,我首先阅读谭浩强c++程序设计一书的ppt,发现第一章基本上都是很基础的东西. 同时,书中与班导师一样,推荐了使用visual c++. 而师爷的教程里面推荐使用的是ec ...

  2. 人工智能——搜索(1)回溯策略【N皇后问题】

    这学期学<人工智能>(马少平,朱小燕 编著)这本书,里面很多算法听老师讲都听不懂,就想试试写一下看看能不能写出来,就从最简单的回溯策略开始吧. 源码 题目描述 在一个n*n的国际象棋棋盘上 ...

  3. ES6新特性4:字符串的扩展

    本文摘自ECMAScript6入门,转载请注明出处. 一.ES5字符串函数 concat: 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf: 返回字符串中一个子串第一处出现的索引 ...

  4. virtualbox+vagrant学习-2(command cli)-11-vagrant PowerShell命令

    PowerShell 格式: vagrant powershell [-- extra powershell args] 这将在主机上打开PowerShell提示符,进入正在运行的vagrant机器. ...

  5. 集合之Map总结

    在前面LZ详细介绍了HashMap.HashTable.TreeMap的实现方法,从数据结构.实现原理.源码分析三个方面进行阐述,对这个三个类应该有了比较清晰的了解,下面LZ就Map做一个简单的总结. ...

  6. java的@PostConstruct注解

    javax.annotation 注释类型 PostConstruct @Documented @Retention(value=RUNTIME) @Target(value=METHOD) publ ...

  7. 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数。定义 Gymnastics 类和 School 类,它们都是 ComputerAverage 的子类。Gymnastics 类中计算选手的平均成绩的方法是去掉一个最低分,去掉一个最高分,然后求平均分;School 中计算平均分的方法是所有科目的分数之和除以总科目数。 要求:定义ComputerAv

    题目: 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数. 定义 Gymnastics 类和 School 类,它们都是 ComputerAverag ...

  8. Kafka设计解析(四)Kafka Consumer设计解析

    转载自 技术世界,原文链接 Kafka设计解析(四)- Kafka Consumer设计解析 目录 一.High Level Consumer 1. Consumer Group 2. High Le ...

  9. PAT乙级1016

    1016 部分A+B (15 分)   正整数 A 的“D​A​​(为 1 位整数)部分”定义为由 A 中所有 D​A​​ 组成的新整数 P​A​​.例如:给定 A=3862767,D​A​​=6,则 ...

  10. Manifest XML signature is not valid(安装ClickOnce签名未通过验证)

    转载:http://stackoverflow.com/questions/12826798/manifest-xml-signature-is-not-valid 安装时,我的问题:  PLATFO ...