BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的,如果没有设置,那么我们就可以打包自定义项,

AssetBundleBuild assetBundleBuild = new AssetBundleBuild();  我们可以new 出来一个 AssetBundleBuild  ,在这里指定他的  assetBundleName  和  assetBundleVariant  ,也就是前面提到的AssetLabels,

同时还要指出此资源所在地址     assetBundleBuild.assetNames = new string[] { path };  并将所有的 AssetBundleBuild  做为数组返回,填入 BuildPipeline.BuildAssetBundles() 所需要的参数中;

详见代码

 static List<AssetBundleBuild> listassets = new List<AssetBundleBuild>();  //第二种打包方式用
static List<DirectoryInfo> listfileinfo = new List<DirectoryInfo>();
static bool isover = false; //是否检查完成,可以打包 public static bool GetState()
{
return isover;
} public static AssetBundleBuild[] GetAssetBundleBuilds()
{
return listassets.ToArray();
} public static void SetAssetBundleBuilds()
{
UnityEngine.Object obj = Selection.activeObject; //选中的物体
string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹 //目录结构 Assets/Resources 没有Application.datapath
SearchFileAssetBundleBuild(path);
} public static void SearchFileAssetBundleBuild(string path) //是文件,继续向下
{
DirectoryInfo directory = new DirectoryInfo(@path);
FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos();
listfileinfo.Clear();
foreach (var item in fileSystemInfos)
{
int idx = item.ToString().LastIndexOf(@"\");
string name = item.ToString().Substring(idx + );
if ((item as DirectoryInfo) != null)
listfileinfo.Add(item as DirectoryInfo);
if (!name.Contains(".meta"))
{
CheckFileOrDirectoryReturnBundleName(item, path + "/" + name);
}
}
if (listfileinfo.Count == )
isover = true;
else
{
Debug.LogError(listfileinfo.Count);
}
} public static string CheckFileOrDirectoryReturnBundleName(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹
{
FileInfo fileInfo = fileSystemInfo as FileInfo;
if (fileInfo != null)
{
string[] strs = path.Split('.');
string[] dictors = strs[].Split('/');
string name = "";
for (int i = ; i < dictors.Length; i++)
{
if (i < dictors.Length - )
{
name += dictors[i] + "/";
}
else
{
name += dictors[i];
}
}
AssetBundleBuild assetBundleBuild = new AssetBundleBuild();
assetBundleBuild.assetBundleName = name;
assetBundleBuild.assetBundleVariant = "bytes";
assetBundleBuild.assetNames = new string[] { path };
listassets.Add(assetBundleBuild);
return name;
}
else
{
SearchFileAssetBundleBuild(path);
return null;
}
}

然后在这里调用

注意,因为文件夹下还可能有文件夹,我们要等所有资源遍历完成,再打包,所以用了一个死循环

 [MenuItem("Tools/打包选中文件夹下所有项目")]
public static void BundSelectionAssets()
{
BuilderAssetsBunlds.SetAssetBundleBuilds();
while (true)
{
if (BuilderAssetsBunlds.GetState())
break;
else
Debug.LogError("等待.....");
}
BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuilderAssetsBunlds.GetAssetBundleBuilds(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); }

好了,今天就到这里,加上前面的,所有的都在这里

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
using System;
using System.Security.Cryptography; public class BuilderAssetsBunlds
{
public static string GetOutAssetsDirecotion() //打包输出地址,后续会再完善
{
string assetBundleDirectory = Application.streamingAssetsPath;
if (!Directory.Exists(assetBundleDirectory))
{
Directory.CreateDirectory(assetBundleDirectory);
}
return assetBundleDirectory;
} /// <summary>
/// 检查目标文件下的文件系统
/// </summary>
public static void CheckFileSystemInfo() //检查目标目录下的文件系统
{
AssetDatabase.RemoveUnusedAssetBundleNames(); //移除没有用的assetbundlename
UnityEngine.Object obj = Selection.activeObject; //选中的物体
string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹 //目录结构 Assets/Resources 没有Application.datapath
CoutineCheck(path);
} public static void CheckFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹
{
FileInfo fileInfo = fileSystemInfo as FileInfo;
if (fileInfo != null)
{
SetBundleName(path);
}
else
{
CoutineCheck(path);
}
} public static void CoutineCheck(string path) //是文件,继续向下
{
DirectoryInfo directory = new DirectoryInfo(@path);
FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos)
{
// Debug.Log(item);
int idx = item.ToString().LastIndexOf(@"\");
string name = item.ToString().Substring(idx + ); if (!name.Contains(".meta"))
{
CheckFileOrDirectory(item, path + "/" + name); //item 文件系统,加相对路径
}
}
} public static void SetBundleName(string path) //设置assetbundle名字
{
// Debug.LogError(path);
var importer = AssetImporter.GetAtPath(path);
string[] strs = path.Split('.');
string[] dictors = strs[].Split('/');
string name = "";
for (int i = ; i < dictors.Length; i++)
{
if (i < dictors.Length - )
{
name += dictors[i] + "/";
}
else
{
name += dictors[i];
}
}
if (importer != null)
{
importer.assetBundleName = name;
importer.assetBundleVariant = "bytes";
// importer.assetBundleName = GetGUID(path); //两种设置方式
}
else
Debug.Log("importer是空的");
} public static string GetGUID(string path)
{
return AssetDatabase.AssetPathToGUID(path);
} //*****************配置文件设置***********************//////////// static Dictionary<string, string> dicversoion = new Dictionary<string, string>(); //版本
static Dictionary<string, string> dicurl = new Dictionary<string, string>(); //下载地址 public static string GetAssetBundleStringPath() //得到存放打包资源的文件路径
{
string path = Application.streamingAssetsPath;
//Debug.Log(path);
string[] strs = path.Split('/');
int idx = ;
for (int i = ; i < strs.Length; i++)
{
if (strs[i] == "Assets")
idx = i;
}
// Debug.Log(idx);
//path = strs[strs.Length - 2] + "/" + strs[strs.Length - 1];
string str = "";
for (int i = idx; i < strs.Length; i++)
{
if (i != strs.Length - )
str += strs[i] + "/";
else if (i == strs.Length - )
str += strs[i];
//Debug.Log(i);
}
path = str;
return path;
//Debug.Log(path);
} public static void CheckAssetBundleDir(string path) //是文件,继续向下
{
DirectoryInfo directory = new DirectoryInfo(@path);
FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos)
{
// Debug.Log(item);
int idx = item.ToString().LastIndexOf(@"\");
string name = item.ToString().Substring(idx + ); if (!name.Contains(".meta"))
{
CheckAssetBundleFileOrDirectory(item, path + "/" + name); //item 文件系统,加相对路径
}
}
} public static void CheckAssetBundleFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹
{
FileInfo fileInfo = fileSystemInfo as FileInfo;
if (fileInfo != null)
{
Debug.Log("不为空,MD5值==>" + GetMD5(path));
// string guid = AssetDatabase.AssetPathToGUID(path);
// Debug.Log("不为空,文件名字是==>>"+guid);
Addconfigdic(fileInfo.Name, GetMD5(path));
}
else
{
CheckAssetBundleDir(path);
}
} public static string GetMD5(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fs);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = ; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
} public static void Addconfigdic(string name, string verMd5)
{
dicversoion.Add(name, verMd5);
} public static void CreateConfigFile(string path) //创建配置文件
{ }
// -------------------------------我是分割线------------------------------------
// 割.............................
static List<AssetBundleBuild> listassets = new List<AssetBundleBuild>(); //第二种打包方式用
static List<DirectoryInfo> listfileinfo = new List<DirectoryInfo>();
static bool isover = false; //是否检查完成,可以打包 public static bool GetState()
{
return isover;
} public static AssetBundleBuild[] GetAssetBundleBuilds()
{
return listassets.ToArray();
} public static void SetAssetBundleBuilds()
{
UnityEngine.Object obj = Selection.activeObject; //选中的物体
string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹 //目录结构 Assets/Resources 没有Application.datapath
SearchFileAssetBundleBuild(path);
} public static void SearchFileAssetBundleBuild(string path) //是文件,继续向下
{
DirectoryInfo directory = new DirectoryInfo(@path);
FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos();
listfileinfo.Clear();
foreach (var item in fileSystemInfos)
{
int idx = item.ToString().LastIndexOf(@"\");
string name = item.ToString().Substring(idx + );
if ((item as DirectoryInfo) != null)
listfileinfo.Add(item as DirectoryInfo);
if (!name.Contains(".meta"))
{
CheckFileOrDirectoryReturnBundleName(item, path + "/" + name);
}
}
if (listfileinfo.Count == )
isover = true;
else
{
Debug.LogError(listfileinfo.Count);
}
} public static string CheckFileOrDirectoryReturnBundleName(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹
{
FileInfo fileInfo = fileSystemInfo as FileInfo;
if (fileInfo != null)
{
string[] strs = path.Split('.');
string[] dictors = strs[].Split('/');
string name = "";
for (int i = ; i < dictors.Length; i++)
{
if (i < dictors.Length - )
{
name += dictors[i] + "/";
}
else
{
name += dictors[i];
}
}
AssetBundleBuild assetBundleBuild = new AssetBundleBuild();
assetBundleBuild.assetBundleName = name;
assetBundleBuild.assetBundleVariant = "bytes";
assetBundleBuild.assetNames = new string[] { path };
listassets.Add(assetBundleBuild);
return name;
}
else
{
SearchFileAssetBundleBuild(path);
return null;
}
} }
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine; public class Constant
{
public static string SD_DIR = "SD_DIR";
}
public class BundleSystem
{
//打包、、、、、、、、、、、、、、、、、、、、、、
[MenuItem("Tools/打包所有设置AssetLable项目")]
public static void BundlerAssets()
{
// Debug.LogError("打包Assets");
BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
} [MenuItem("Tools/打包选中文件夹下所有项目")]
public static void BundSelectionAssets()
{
BuilderAssetsBunlds.SetAssetBundleBuilds();
while (true)
{
if (BuilderAssetsBunlds.GetState())
break;
else
Debug.LogError("等待.....");
}
BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuilderAssetsBunlds.GetAssetBundleBuilds(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } [MenuItem("Tools/设置Assetbundle名字")]
public static void SetAssetBundellabls()
{
BuilderAssetsBunlds.CheckFileSystemInfo();
} [MenuItem("Tools/设置配置文件")]
public static void SetAssetConfig()
{
BuilderAssetsBunlds.CheckAssetBundleDir(BuilderAssetsBunlds.GetAssetBundleStringPath());
} [MenuItem("Tools/测试")]
static void PackAsset()
{
Debug.Log("Make AssetsBundle");
//
List<AssetBundleBuild> builds = new List<AssetBundleBuild>();
AssetBundleBuild build = new AssetBundleBuild(); build.assetBundleName = "first";
build.assetBundleVariant = "u3";
// build.assetNames[0] = "Assets/Resources/mascot.prefab";
build.assetNames = new string[] { "Assets/PNG/结算副本.png" };
builds.Add(build);
// 第一个参数为打包文件的输出路径,第二个参数为打包资源的列表,第三个参数为打包需要的操作,第四个为打包的输出的环境
//BuildPipeline.BuildAssetBundles(@"Assets/Bundle", builds.ToArray(),
// BuildAssetBundleOptions.None, BuildTarget.Android);
BuildPipeline.BuildAssetBundles(@"Assets/Bundle", builds.ToArray(), BuildAssetBundleOptions.None, BuildTarget.Android);
Debug.Log("" + builds.ToArray() + "");
}
}

上篇传送门

http://www.cnblogs.com/lzy575566/p/7714510.html

前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle的更多相关文章

  1. 前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误。    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected est

    前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: ...

  2. VS2010 打包生成exe文件后 执行安装文件出现 TODO:&lt;文件说明&gt;已停止工作并已关闭

    一.VS2010 打包生成exe文件后  执行安装文件出现  TODO:<文件说明>已停止工作并已关闭 TODO: <文件说明>已停止工作 原因: 打包的时候在文件系统中建立了 ...

  3. Java+Selenium 3.x 实现Web自动化 - Maven打包TestNG,利用jenkins执行测试

    1. Jenkins本地执行测试 or 服务器端执行测试 测试代码计划通过jenkins执行时,通过网上查询各种教程,大多数为本地执行测试,由此可见,本地执行是大多数人的选择. 经过探讨,最终决定采用 ...

  4. 使用maven-shade-plugin打包spring项目为可执行的jar包

    使用maven-shade-plugin打包spring项目为可执行的jar包,打包后的jar包里面包含依赖的jar包. POM文件: <plugin> <groupId>or ...

  5. 什么是cookie(前段时间看到别人简历上把cookie和localStorage混淆了所以专门又去了解了下)

    在前端面试中,有一个必问的问题:请你谈谈cookie和localStorage有什么区别啊? localStorage是H5中的一种浏览器本地存储方式,而实际上,cookie本身并不是用来做服务器存储 ...

  6. 如何实现 Https拦截进行 非常规“抓包” 珍惜Any 看雪学院 今天 前段时间在自己做开发的时候发现一个很好用的工具,OKHttp的拦截器(何为拦截器?就是在每次发送网络请求的时候都会走的一个回调)大概效果如下:

    如何实现 Https拦截进行 非常规“抓包” 珍惜Any 看雪学院 今天 前段时间在自己做开发的时候发现一个很好用的工具,OKHttp的拦截器(何为拦截器?就是在每次发送网络请求的时候都会走的一个回调 ...

  7. 一次I/O问题引发的P0重大故障[改版重推] 原创 二马读书 二马读书 8月16日 这是前段时间发的一篇文章,很多读者反馈,文章没有揭示故障发生的详细

    一次I/O问题引发的P0重大故障[改版重推] 原创 二马读书 二马读书 8月16日 这是前段时间发的一篇文章,很多读者反馈,文章没有揭示故障发生的详细

  8. 关于IDEA每次修改HTML,Css等静态资源文件都需要重启的设置修改

    ~ ~ ~ ~ ~ 关于IDEA每次修改HTML,Css等静态资源文件都需要重启的设置修改 最近开始使用IDEA进行项目开发,但是对于每次修改HTML文件中css和js文件之后都必须重启服务这件事表示 ...

  9. linux下设置计划任务执行python脚本

    linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...

随机推荐

  1. PYTHON MYSQL 的表创建和插入

    import mysql.connector cnx = mysql.connector.connect(user='xx',password='xx++.',host='139.107.11.166 ...

  2. Android自己主动化构建之Ant多渠道打包实践(下)

    前言 上一篇(Android自己主动化构建之Ant多渠道打包实践(上))已经介绍了Android的apk是怎样构建的,本篇博客继续Ant打包的实践过程. 集成友盟统计SDK 这里以友盟统计为例,对各个 ...

  3. hibernate 懒加载图解

  4. Android中android:layout_alignParentBottom标签不生效

    Android中一个奇怪的问题 Android 6.0中android:layout_alignParentBottom设置后不生效. 详见:https://code.google.com/p/and ...

  5. Python 内置函数、作用域、闭包、递归

    一.内置函数如何使用 help()一下: 如想看min()咋用?在shell中:help(min) 二.部分内置函数 (一).排序:sorted() li = [(1, 2, 3, 4), (7, 8 ...

  6. BZOJ 2431

    2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1521  Solved: 883[Submit][Statu ...

  7. EasyNVR摄像机无插件直播进行摄像机云台控制的接入及调用详解

    EasyNVR云台接入及控制详解 摄像机云台控制在摄像机当中很常见摄像机能将当前状态下云台的水平角度.倾斜角度和摄像机镜头焦距等位置参数存储到设备中,需要时可以迅速调用这些参数并将云台和摄像头调整至该 ...

  8. SQL获取某个时间字符串里的月和日,获取某天是周几

    select datename(weekday,'2016-11-4') as '周' select convert(varchar,datepart(month,'2016-11-4')) as ' ...

  9. Spring3.2.11与Quartz2.2.1整合时内存泄漏的问题的解决

    Quartz是一款定时任务调度的开源框架,使用起来比较方便.并且Spring的support包对Quartz有集成.但是笔者在web应用使用的过程中却遇到了内存泄漏的问题. 问题的产生 笔者在使用Sp ...

  10. 巨蟒python全栈开发linux之centos1

    1.linux服务器介绍 2.linux介绍 3.linux命令学习 linux默认有一个超级用户root,就是linux的皇帝 注意:我的用户名是s18,密码是centos 我们输入密码,点击解锁( ...