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研究院之全面理解图集与使用(三)》,做了部分修改。

  1. #define USE_ASSETBUNDLE
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5. using UnityEngine.UI;
  6. using UnityEditor.VersionControl;
  7.  
  8. public class UIMain : MonoBehaviour{
  9.  
  10. AssetBundle assetbundle = null;
  11. void Start ()
  12. {
  13. CreatImage(loadSprite("flag_blue"));
  14. CreatImage(loadSprite("flag_yellow"));
  15. }
  16.  
  17. private void CreatImage(GameObject gobj ){
  18. Sprite sprite = gobj.GetComponent<SpriteRenderer>().sprite as Sprite;
  19. GameObject go = new GameObject(sprite.name);
  20. go.layer = LayerMask.NameToLayer("UI");
  21. go.transform.parent = transform;
  22. go.transform.localScale= Vector3.one;
  23. Image image = go.AddComponent<Image>();
  24. image.sprite = sprite;
  25. image.SetNativeSize();
  26. }
  27.  
  28. private GameObject loadSprite(string spriteName){
  29. #if USE_ASSETBUNDLE
  30. if(assetbundle == null)
  31. assetbundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath +"/flagbundle");
  32. return assetbundle.LoadAsset(spriteName) as GameObject;
  33. #else
  34. return Resources.Load<GameObject>("Sprite/" + spriteName);
  35. #endif
  36. }
  37.  
  38. }

UIMain

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using System.IO;
  6. using UnityEditor.VersionControl;
  7.  
  8. public class AltasMaker : MonoBehaviour {
  9.  
  10. [MenuItem ("MyMenu/AtlasMaker")]
  11. static private void MakeAtlas()
  12. {
  13. string spriteDir = Application.dataPath + "/Resources/Sprite";
  14. Debug.Log("spriteDir : " + spriteDir);
  15. if(!Directory.Exists(spriteDir)){
  16. Directory.CreateDirectory(spriteDir);
  17. }
  18.  
  19. DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath + "/Atlas");
  20. foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
  21. foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories)) {
  22. string allPath = pngFile.FullName;
  23. Debug.Log("allPath1 : " + allPath);
  24. string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
  25. Debug.Log("assetPath : " + assetPath);
  26. Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
  27. GameObject go = new GameObject(sprite.name);
  28. go.AddComponent<SpriteRenderer>().sprite = sprite;
  29. allPath = spriteDir+ "/" +sprite.name+ ".prefab";
  30. Debug.Log("allPath2 : " + allPath);
  31. string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));
  32. PrefabUtility.CreatePrefab(prefabPath, go);
  33. GameObject.DestroyImmediate(go);
  34. }
  35. }
  36. }
  37.  
  38. [MenuItem ("MyMenu/Build Assetbundle")]
  39. static private void BuildAssetBundle()
  40. {
  41. string outputdir = Application.dataPath + "/StreamingAssets";
  42.  
  43. if(!Directory.Exists(outputdir))
  44. {
  45. Directory.CreateDirectory(outputdir);
  46. }
  47.  
  48. DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath + "/Atlas");
  49. foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
  50. List<Sprite> assets = new List<Sprite>();
  51.  
  52. foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories))
  53. {
  54. string allPath = pngFile.FullName;
  55. string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
  56. assets.Add(AssetDatabase.LoadAssetAtPath<Sprite>(assetPath));
  57. }
  58.  
  59. AssetBundleBuild[] buildMap = new AssetBundleBuild[];
  60.  
  61. buildMap[].assetBundleName = dirInfo.Name + "bundle"; // 想定义的任何名称
  62. string[] spriteNames = new string[assets.Count];
  63. for(int i = ; i < assets.Count; i++)
  64. {
  65. spriteNames[i] = "Assets/Atlas/" + dirInfo.Name + "/" + assets[i].name + ".png"; // 注意路径写全
  66. }
  67. buildMap[].assetNames = spriteNames;
  68. buildMap[].assetBundleVariant = null;
  69.  
  70. BuildPipeline.BuildAssetBundles(
  71. outputdir, // output path
  72. buildMap, // build bundles info
  73. BuildAssetBundleOptions.UncompressedAssetBundle, // options
  74. GetBuildTarget()); // build target
  75. }
  76. }
  77.  
  78. [MenuItem ("MyMenu/Build All AssetBundles")]
  79. static private void BuildAllAssetBundles()
  80. {
  81. BuildPipeline.BuildAssetBundles(
  82. Application.dataPath + "/StreamingAssets", // output path
  83. BuildAssetBundleOptions.ChunkBasedCompression, // options
  84. GetBuildTarget());
  85. }
  86.  
  87. static private BuildTarget GetBuildTarget()
  88. {
  89. BuildTarget target = BuildTarget.WebPlayer;
  90. #if UNITY_STANDALONE
  91. target = BuildTarget.StandaloneOSXIntel64;
  92. #elif UNITY_IPHONE
  93. target = BuildTarget.iPhone;
  94. #elif UNITY_ANDROID
  95. target = BuildTarget.Android;
  96. #endif
  97. return target;
  98. }
  99. }

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. Python中的赋值和拷贝

    赋值 在python中,赋值就是建立一个对象的引用,而不是将对象存储为另一个副本.比如: >>> a=[1,2,3] >>> b=a >>> c= ...

  2. Hive学习之路 (四)Hive的连接3种连接方式

    一.CLI连接 进入到 bin 目录下,直接输入命令: [hadoop@hadoop3 ~]$ hive SLF4J: Class path contains multiple SLF4J bindi ...

  3. c++中内存拷贝函数(C++ memcpy)详解

    原型:void*memcpy(void*dest, const void*src,unsigned int count); 功能:由src所指内存区域复制count个字节到dest所指内存区域. 说明 ...

  4. selenium 无界面跑UI脚本

    from selenium.webdriver.chrome.options import Options from selenium import webdriver import time chr ...

  5. PAT乙级1004

    1004 成绩排名 (20 分)   读入 n(>0)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用例,格式为 第 1 行:正 ...

  6. MapReduce经典入门小案例

    /** * 单词统计 * @author fengmingyue * */ public class WordCount { public static void main(String[] args ...

  7. FFmpeg中几个结构体的意义

    AVCodec是存储编解码器信息的结构体,特指一个特定的解码器,比如H264编码器的名字,ID,支持的视频格式,支持的采样率等: AVCodecContext是一个描述编解码器采用的具体参数,比如采用 ...

  8. Scala-字符串操作

    package com.bigdata object StringO { def main(args: Array[String]): Unit = { val s1 = "Hello&qu ...

  9. Installation failed: Timeout was reached: Operation timed out after 10000 milliseconds with 0 out of 0 bytes received

    Trying this option worked for me. library(httr) with_config(use_proxy(...), install_github(...)) OR ...

  10. 6.Exceptions-异常(Dart中文文档)

    异常是用于标识程序发生未知异常.如果异常没有被捕获,If the exception isn't caught, the isolate that raised the exception is su ...