怎么把一个文件夹unity打包文件夹成.unity3d格式的文件?

查看: 8199|回复: 6
最后登录注册时间阅读权限90积分32965
资深设计师, 积分 32965, 距离下一级还需 17035 积分
纳金币32806 精华12
// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to &Menu& -& &Assets& and select one of the choices
// to build the Asset Bundle
using UnityE
using UnityE
using System.IO;
public class ExportAssetBundles {
[MenuItem(&Assets/Build AssetBundle From Selection - Track dependencies&)]//(包含依耐关系)
static void ExportResource () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel (&Save Resource&, &&, &New Resource&, &&);
if (path.Length != 0) {
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | pleteAssets);
Selection.objects =
FileStream fs = File.Open(path+&.log&, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(&文件 & + path + & 中的内容如下:&);
foreach (Object obj in Selection.objects)
sw.WriteLine(&Name: & + obj.name + && & & & & & & & & & & & Type:& + obj.GetType());
if (obj.GetType() == typeof(Object))
Debug.LogWarning(&Name: & + obj.name + &, Type: & + obj.GetType() + &. 可能是unity3d不能识别的文件,可能未被打包成功&);
sw.Flush();
fs.Flush();
sw.Close();
fs.Close();
BuildAssetBundleOptions.CollectDependencies 包含所有依赖关系,应该是将这个物体或组件所使用的其他资源一并打包
pleteAssets 强制包括整个资源。例如,如果传递网格到BuildPipeline.BuildAssetBundle函数并使用CompleteAssets,它还将包括游戏物体和任意动画剪辑,在同样的资源。
应该是,下载一部分,其余一并下载
BuildAssetBundleOptions.DisableWriteTypeTree 禁用写入类型树,在资源包不包含类型信息。指定这个标识将使资源包易被脚本或Unity版本改变,但会使文件更小,更快一点加载。这个标识只影响默认包含的类型信息的平台资源包。
BuildAssetBundleOptions.DeterministicAssetBundle 确定资源包,编译资源包使用一个哈希表储存对象ID在资源包中。
这使您可以重建的资产包,并直接引用资源。当重建资源包对象,在重建之后保证有相同的ID。由于它是一个32位的哈希表空间,如果在资源包有许多对象,这将增加潜在哈希表冲突。
Unity将给出一个错误,而不在这种情况下编译。哈希表是基于资源的GUID和对象的自身ID。
DeterministicAssetBundle加载被标准资源包慢,这是因为线程后台加载API通常期望对象排序的方式,这会使读取加少寻址。
System.GC.Collect();
[MenuItem(&Assets/Build AssetBundle From Selection - No dependency tracking&)]//(不包含依耐关系)
static void ExportResourceNoTrack () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel (&Save Resource&, &&, &New Resource&, &unity3d&);
if (path.Length != 0)
// Build the resource file from the active selection.
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path, pleteAssets,BuildTarget.StandaloneWindows);
FileStream fs = File.Open(path + &.log&, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(&文件 & + path + & 中的内容如下:&);
foreach (Object obj in Selection.objects)
sw.WriteLine(&Name: & + obj.name + && & & & & & & & & & & & Type: & + obj.GetType());
if (obj.GetType() == typeof(Object))
Debug.LogWarning(&Name: & + obj.name + &, Type: & + obj.GetType() + &. 可能是unity3d不能识别的文件,可能未被打包成功&);
sw.Flush();
fs.Flush();
sw.Close();
fs.Close();
System.GC.Collect();
//------------------------------------------------------------------------------------------------------------------
//单独打包文件夹中的每个文件为*.unity3d文件,放在原来的位置
[MenuItem(&Assets/Build AssetBundles From Directory of Files&)]
static void ExportAssetBundleEachfile2Path()
//在项目视图从选择的文件夹生成资源包
//记住,这个函数不跟踪依赖关系,也不是递归
// Get the selected directory
//获取选择的目录
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
string unity3dFileN
Debug.Log(&Selected Folder: & + path);
if (path.Length != 0)
int pos=path.LastIndexOf('/');
if (pos == -1)
unity3dFileName = path.Substring(pos+1,path.Length-pos-1);
Debug.Log(&unity3dFileName: & + unity3dFileName);
path = path.Replace(&Assets/&, &&);
string[] fileEntries = Directory.GetFiles(Application.dataPath + &/& + path);
foreach (string fileName in fileEntries)
string filePath = fileName.Replace(&&, &/&);
int index = filePath.LastIndexOf(&/&);
filePath = filePath.Substring(index);
Debug.Log(filePath);
string localPath = &Assets/& +
if (index & 0)
localPath += fileP
Object t = AssetDatabase.LoadMainAssetAtPath(localPath);
if (t != null)
Debug.Log(t.name);
string bundlePath = &Assets/& + path + &/& + t.name + &.unity3d&;
Debug.Log(&Building bundle at: & + bundlePath);
// Build the resource file from the active selection.
//从激活的选择编译资源文件
BuildPipeline.BuildAssetBundle
(t, null, bundlePath, pleteAssets);
System.GC.Collect();
[MenuItem(&Assets/Build AssetBundles by Directory&)]
static void ExportAssetBundleByDirectory()
//一次选择多个文件夹,分别打包,生成的unity3d文件按文件夹名字命名且放在文件夹中,同时生成.log文件
int dirs = Selection.objects.Length,i;
string[] filters = new string[]
{//过滤不打包的文件类型
&.unity3d&,
for (i = 0; i & i++)//处理所选文件夹
string path = AssetDatabase.GetAssetPath(Selection.objects);
string unity3dFileN
if (path.Length != 0)
int pos = path.LastIndexOf('/');
if (pos&0) pos = 0;
unity3dFileName = path.Substring(pos);
//获取文件列表
string[] fileEntries = Directory.GetFiles(path, &*.*&, SearchOption.TopDirectoryOnly);//仅本级目录
//过滤文件类型
int size = 0;
bool[] enable=new bool[fileEntries.Length];
for ( int how = 0; how & fileEntries.L how++)
bool filterFlag =
foreach (string s in filters)
if (fileEntries[how].EndsWith(s, System.StringComparison.OrdinalIgnoreCase))
filterFlag = ***e;
enable[how] = filterF
if (!filterFlag) size++;
if (size != 0)
Object[] objects = new Object[size];
//载入文件
int id = 0;
for (int k = 0; k & fileEntries.L k++)
if (enable[k] == false)
string fileName = fileEntries[k];
string localPath = fileName.Replace(&&, &/&);
objects[id] = AssetDatabase.LoadMainAssetAtPath(localPath);//AssetDatabase.LoadAllAssetsAtPath不知为何不能用?
if (id != 0)
string str = path + unity3dFileName + &.unity3d&;
BuildPipeline.BuildAssetBundle(objects[0], objects, str, pleteAssets,BuildTarget.StandaloneWindows);
FileStream fs = File.Open(path + unity3dFileName + &.log&, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(&文件 & + str + & 中的内容如下:&);
foreach (Object obj in objects)
sw.WriteLine(&Name: & + obj.name + && & & & & & & & & & & & Type: & + obj.GetType());
if (obj.GetType() == typeof(Object))
Debug.LogWarning(&Name: & + obj.name + &, Type: & + obj.GetType()+&. 可能是unity3d不能识别的文件,可能未被打包成功&);
sw.Flush();
fs.Flush();
sw.Close();
fs.Close();
Debug.Log(&打包成功! & + str);
Debug.LogError(&没有可打包的文件! 目录:& + path);
Debug.LogError(&没有可打包的文件! 全部被过滤, 目录:& + path);
System.GC.Collect();
//------------------------------------------------------------------------------------------------------------------
==========================================================================
改进一下,由于unity3d会自动导入新生成的*.unity3d和*.log文件,所以最好把输出目录改到Assets外面
if (id != 0)
string outpath = System.IO.Directory.GetCurrentDirectory() + &/unity3d-result& + path.Substring(path.IndexOf('/'));//复制文件夹结构
//string outpath = System.IO.Directory.GetCurrentDirectory() + &/unity3d-result& ;//不复制文件夹结构
if (!System.IO.Directory.Exists(outpath)) System.IO.Directory.CreateDirectory(outpath); //string outpath =
string str = outpath + unity3dFileName + &.unity3d&;
// Build the resource file from the active selection.
BuildPipeline.BuildAssetBundle(objects[0], objects, str);
FileStream fs = File.Open(outpath + unity3dFileName + &.log&, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(&文件 & + str + & 中的内容如下:&);
foreach (Object obj in objects)
sw.WriteLine(&Name: & + obj.name + && & & & & & & & & & & & Type: & + obj.GetType());
if (obj.GetType() == typeof(Object))
Debug.LogWarning(&Name: & + obj.name + &, Type: & + obj.GetType()+&. 可能是unity3d不能识别的文件,可能未被打包成功&);
sw.Flush();
fs.Flush();
sw.Close();
fs.Close();
Debug.Log(&打包成功! & + str);
Debug.LogError(&没有可打包的文件! 目录:& + path);
(可以按文件夹打包,方便提高效率)
10:43 上传
点击文件名下载附件
0 Bytes, 下载次数: 323
最后登录注册时间阅读权限90积分32965
资深设计师, 积分 32965, 距离下一级还需 17035 积分
纳金币32806 精华12
转载过来的,希望对爱好者们有帮助
最后登录注册时间阅读权限90积分19585
资深设计师, 积分 19585, 距离下一级还需 30415 积分
纳金币9110 精华35
过来学习学习
最后登录注册时间阅读权限20积分206
设计实习生, 积分 206, 距离下一级还需 294 积分
纳金币6 精华0
请教个问题啊
按照官网上的API说明,BuildAssetBundleOptions.DeterministicAssetBundle设置会重建之后保证有相同的ID,这个“相同的ID”在哪里啊?怎么取出来呀?
最后登录注册时间阅读权限90积分25524
资深设计师, 积分 25524, 距离下一级还需 24476 积分
纳金币396 精华0
谢谢楼主分享
最后登录注册时间阅读权限20积分197
设计实习生, 积分 197, 距离下一级还需 303 积分
纳金币20 精华0
........为什么我选择一个好像所有的资源都打包了!
最后登录注册时间阅读权限10积分68
设计初学者, 积分 68, 距离下一级还需 32 积分
纳金币2 精华0
好用的插件!!!
站长推荐 /1
纳金名模第100期货架3d模型:数量50个,大小为426MB。完全免费下载哦!这样的福利我和我的小伙伴们都惊呆了!!下载地址:
400-067-3919
Powered by - X2.5
Narkii Inc.下次自动登录
现在的位置:
& 综合 & 正文
unity3d大型手游
可以打包obb文件
用unity3d开发手游,有个很大的问题就是apk的size太大, 如果超过50M,一般很多平台就不会肯上线。
一个好的方法是把app打成apk + obb数据包的方式。
1. 编译成obb数据包的方式 unity3d 4.x上有很简单的手法, 选一个checkbox即可
2. obb文件的下载 可以在unity3d中做, 如果用到android project做壳子, 也可以在android中做, 在launchActivity中判断一下数据包是否存在,然后
启动一个后台线程下载obb文件,通过handler和activity通信,刷进度条。
3. obb文件要存成 /mnt/sdcard/Android/obb/packagename/main.versioncode.packagename.obb
4. 下载成功之后重启一下app, unity3d 4.0以后就会自动读取这个obb文件, 游戏就可以正常玩耍了。
writen by tony_蒋
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 unity3d android 打包 的文章

 

随机推荐