查看: 13722|回复: 6
打印 上一主题 下一主题

[经验分享] 改造的unity3d文件打包脚本

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-11-3 10:43: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 UnityEngine;

using UnityEditor;

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", "unity3d");

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 | BuildAssetBundleOptions.CompleteAssets);

Selection.objects = selection;

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 包含所有依赖关系,应该是将这个物体或组件所使用的其他资源一并打包



BuildAssetBundleOptions.CompleteAssets 强制包括整个资源。例如,如果传递网格到BuildPipeline.BuildAssetBundle函数并使用CompleteAssets,它还将包括游戏物体和任意动画剪辑,在同样的资源。

应该是,下载一部分,其余一并下载

BuildAssetBundleOptions.DisableWriteTypeTree 禁用写入类型树,在资源包不包含类型信息。指定这个标识将使资源包易被脚本或Unity版本改变,但会使文件更小,更快一点加载。这个标识只影响默认包含的类型信息的平台资源包。

BuildAssetBundleOptions.DeterministicAssetBundle 确定资源包,编译资源包使用一个哈希表储存对象ID在资源包中。

这使您可以重建的资产包,并直接引用资源。当重建资源包对象,在重建之后保证有相同的ID。由于它是一个32位的哈希表空间,如果在资源包有许多对象,这将增加潜在哈希表冲突。

Unity将给出一个错误,而不在这种情况下编译。哈希表是基于资源的GUID和对象的自身ID。

DeterministicAssetBundle加载被标准资源包慢,这是因为线程后台加载API通常期望对象排序的方式,这会使读取加少寻址。

*/

//GC

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, BuildAssetBundleOptions.CompleteAssets,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();

}

//GC

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 unity3dFileName;

Debug.Log("Selected Folder: " + path);

if (path.Length != 0)

{

int pos=path.LastIndexOf('/');

if (pos == -1)

{

return;

}

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/" + path;

if (index > 0)

localPath += filePath;

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, BuildAssetBundleOptions.CompleteAssets);

}

}

}

//GC

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",

".log",

".db",

};



for (i = 0; i < dirs; i++)//处理所选文件夹

{

string path = AssetDatabase.GetAssetPath(Selection.objects);

string unity3dFileName;

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.Length; how++)

{

bool filterFlag = false;

foreach (string s in filters)

{

if (fileEntries[how].EndsWith(s, System.StringComparison.OrdinalIgnoreCase))

{//=

filterFlag = ***e;

break;

}

}

enable[how] = filterFlag;

if (!filterFlag) size++;

}

if (size != 0)

{

Object[] objects = new Object[size];

//载入文件

int id = 0;

for (int k = 0; k < fileEntries.Length; k++)

{

if (enable[k] == false)

{

string fileName = fileEntries[k];

string localPath = fileName.Replace("", "/");

objects[id] = AssetDatabase.LoadMainAssetAtPath(localPath);//AssetDatabase.LoadAllAssetsAtPath不知为何不能用?

id++;

}

}

//打包

if (id != 0)

{

string str = path + unity3dFileName + ".unity3d";

BuildPipeline.BuildAssetBundle(objects[0], objects, str, BuildAssetBundleOptions.CompleteAssets,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);

}

else

{

Debug.LogError("没有可打包的文件! 目录:" + path);

}

}

else

{

Debug.LogError("没有可打包的文件! 全部被过滤, 目录:" + path);

}

}

}

//GC

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 = path;

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);

}

else

{

Debug.LogError("没有可打包的文件! 目录:" + path);

}



(可以按文件夹打包,方便提高效率)

ExportAssetBundles.rar

0 Bytes, 下载次数: 341

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-11-3 11:53:52 |只看该作者
转载过来的,希望对爱好者们有帮助
回复

使用道具 举报

may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52328
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

板凳
发表于 2012-11-3 12:30:25 |只看该作者
过来学习学习
回复

使用道具 举报

0

主题

1

听众

206

积分

设计实习生

Rank: 2

纳金币
6
精华
0

最佳新人

地板
发表于 2013-8-12 09:38:52 |只看该作者
请教个问题啊
按照官网上的API说明,BuildAssetBundleOptions.DeterministicAssetBundle设置会重建之后保证有相同的ID,这个“相同的ID”在哪里啊?怎么取出来呀?
回复

使用道具 举报

40

主题

0

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
279
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2013-8-13 02:57:06 |只看该作者
谢谢楼主分享
回复

使用道具 举报

0

主题

1

听众

211

积分

设计实习生

Rank: 2

纳金币
18
精华
0

最佳新人

6#
发表于 2013-8-24 10:34:54 |只看该作者
........为什么我选择一个好像所有的资源都打包了!
回复

使用道具 举报

0

主题

1

听众

72

积分

设计初学者

Rank: 1

纳金币
2
精华
0

活跃会员 灌水之王

7#
发表于 2013-8-26 15:17:39 |只看该作者
好用的插件!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 16:26 , Processed in 0.521072 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部