原址:http://blog.csdn.net/libeifs/article/details/7310962
羽化的第二十六篇博客,春节回来后工作就十分繁忙,一直没时间来整理博客,Unity升级到了3.5没遇到任何问题,新功能中粒子系统很有趣,以后写篇博客研究下~ ~ 最近在整理这个下载规则的时候才能写一篇关于打包下载的博客,只能给大家做参考,没多少技术含量,真正复杂的的是如何在智能化上。就羽化目前接触的Unity3D网页游戏而言,很多都是以一个Prefab为单位来打包,或者是Scene来打包,这种作法很方便,但不够完美,有一套完整的加载和打包结构才是最佳的网页解决方案。
PSV看来很吸引人可惜买不起。。。PSP末期大作中最终幻想零式素质惊人,说是PSP上最强游戏绝对无可厚非,Square在进化最终幻想同时,也进化了RPG的表现形式,大部分人接受了时代的变迁,少部分人沉浸在旧时的RPG游戏中,但不可否认的事,游戏慢慢成了最佳的艺术表现形式。
2012年2月22日这种特殊的节日适合发生一些特殊的事,时光荏苒不知道能不能用上,只知道这几年,自己从未改变,以后估计也不会改变,拖着这个沉重的身躯,也要赶上这个过快的世界,虽然才离开家不久,但又开始想家了,可惜有段时间回不去了。
本次学习:
1. 简介物件组成
2. AssetBundle
3. 打包和读取
1.简介物件组成
这是羽化做的一个粗略图,其中各个组成部分除了工程以外都能通过AssetBundle打成一个后缀名为.unity3d的外部资源包,通过Unity自身的高压技术,最多能节约60%~70%的资源容量。
2.AssetBundle
官方有自带的AssetBundle的例子,羽化通过这个例子改成了自己的一个小工程,通过AssetBundle的打包处理的好处在于把自身的依赖关系包括数据结构合理的分配完整,这样不需要开发者自己去给下载或者加载的资源控制路径,大大降低了开发大型在线游戏的门槛,同时越来越细的封包可以减少游戏的总体大小,资源最大化利用,对于小游戏也带来了很大帮助,可惜按照官方的说法是目前不能完全支持移动端。
AssetBundle具体使用方法在手册上写得很详细,圣典里也有详细的中文说明,羽化就不多说废话,需要注意的是AssetBundle的读取,虽然资源基本上都是用简单读取,但在特殊情况下会去读取特定unity3d包中的特定贴图,或者说是模型,这里就必须注意打包的结构,因为读取出来的是一个Object数组,可以使用Load读取特定资源。
3. 打包和读取
打包方式是在Project视图下选择AutomatedExportResources,过程是自动完成,Unity的Editor类中提供了很多有趣的方法可以实现不同的打包操作,官方只给了手选打包和自动打包两种,但都不够智能。
AutomatedExportResources.js
- <SPAN style="FONT-SIZE: 16px">@MenuItem("Assets/Auto Build Resource Files")
- static function ExportResource ()
- {
- System.IO.Directory.CreateDirectory("AssetBundles");
- var options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets;
- BuildPipeline.PushAssetDependencies();
- /*Effects*/
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Texture/Effects/Hit/Hit1.tga"), null, "AssetBundles/Data/Texture/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Texture/Effects/Hit/Hit2.tga"), null, "AssetBundles/Data/Texture/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Model/Effects/Hit/Hit1.fbx"), null, "AssetBundles/Data/Model/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Model/Effects/Hit/Hit2.fbx"), null, "AssetBundles/Data/Model/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Material/Effects/Hit/Hit1.mat"), null, "AssetBundles/Data/Material/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Material/Effects/Hit/Hit2.mat"), null, "AssetBundles/Data/Material/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit1.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit1.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit2.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit2.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit3.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit3.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PushAssetDependencies();
- BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit3.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit3.unity3d", options);
- BuildPipeline.PopAssetDependencies();
- BuildPipeline.PopAssetDependencies();
- }</SPAN>
@MenuItem("Assets/Auto Build Resource Files")
static function ExportResource ()
{
System.IO.Directory.CreateDirectory("AssetBundles");
var options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets;
BuildPipeline.PushAssetDependencies();
/*Effects*/
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Texture/Effects/Hit/Hit1.tga"), null, "AssetBundles/Data/Texture/Effects/Hit/Hit1.unity3d", options);
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Texture/Effects/Hit/Hit2.tga"), null, "AssetBundles/Data/Texture/Effects/Hit/Hit2.unity3d", options);
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Model/Effects/Hit/Hit1.fbx"), null, "AssetBundles/Data/Model/Effects/Hit/Hit1.unity3d", options);
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Model/Effects/Hit/Hit2.fbx"), null, "AssetBundles/Data/Model/Effects/Hit/Hit2.unity3d", options);
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Material/Effects/Hit/Hit1.mat"), null, "AssetBundles/Data/Material/Effects/Hit/Hit1.unity3d", options);
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Material/Effects/Hit/Hit2.mat"), null, "AssetBundles/Data/Material/Effects/Hit/Hit2.unity3d", options);
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit1.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit1.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit2.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit2.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit3.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit3.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Standard Assets/Data/Prefab/Effects/Hit/Hit3.prefab"), null, "AssetBundles/Data/Prefab/Effects/Hit/Hit3.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
}
这是羽化的打包代码,可以看出羽化做了一个两层依赖,当然可以做更多,但写起也就更加复杂,这种打包实现的是预设与其他物件分离,其他物件包括材质球、模型、贴图,材质球中包含着色器和属性信息,贴图往往是一张tga,模型是一个FBX,这样打包羽化认为基本上是最省配置,但要注意加载的时候,在有预设的情况下,最后先下载材质球再下载模型。
Prefab.js
- <SPAN style="FONT-SIZE: 16px">var download : WWW;
- var url = "packed_resource.unity3d";
- var resourcePath = "Lerpz";
- var guiOffset = 20;
- var assetBundle : AssetBundle;
- var instanced : Object;
- function StartDownload () {
- if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
- download = new WWW (url);
- else if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
- download = new WWW ("../AssetBundles/" + url);
- else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
- download = new WWW ("file://" + Application.dataPath + "/../AssetBundles/" + url);
- yield download;
- assetBundle = download.assetBundle;
- if (assetBundle != null)
- {
- // Alternatively you can also load an asset by name (assetBundle.Load("my asset name"))
- var go : Object = assetBundle.mainAsset;
- if (go != null)
- {
- instanced = Instantiate(go);
- }
- else
- Debug.Log("Couldnt load resource");
- }
- else
- {
- Debug.Log("Couldnt load resource");
- }
- }
- function OnGUI()
- {
- GUILayout.Space(guiOffset);
- GUILayout.BeginHorizontal();
- if (download == null)
- {
- if (GUILayout.Button("Download " + url))
- StartDownload();
- }
- else
- {
- if (download.error == null)
- {
- var progress = parseInt(download.progress * 100);
- GUILayout.Label(progress + "%");
- if (download.isDone && GUILayout.Button("Unload Resource") )
- {
- // Destroy the instantiated object
- Destroy(instanced);
- // Dispose the WWW class
- // (This happens automatically from the GC, but you can do it explicitly to make sure it happens early on)
- download.Dispose();
- download = null;
- // Unload the whole asset bundles and any loaded assets
- assetBundle.Unload (true);
- assetBundle = null;
- }
- }
- else
- {
- GUILayout.Label(download.error);
- }
- }
- GUILayout.EndHorizontal();
- }</SPAN>
var download : WWW;
var url = "packed_resource.unity3d";
var resourcePath = "Lerpz";
var guiOffset = 20;
var assetBundle : AssetBundle;
var instanced : Object;
function StartDownload () {
if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
download = new WWW (url);
else if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
download = new WWW ("../AssetBundles/" + url);
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsEditor)
download = new WWW ("file://" + Application.dataPath + "/../AssetBundles/" + url);
yield download;
assetBundle = download.assetBundle;
if (assetBundle != null)
{
// Alternatively you can also load an asset by name (assetBundle.Load("my asset name"))
var go : Object = assetBundle.mainAsset;
if (go != null)
{
instanced = Instantiate(go);
}
else
Debug.Log("Couldnt load resource");
}
else
{
Debug.Log("Couldnt load resource");
}
}
function OnGUI()
{
GUILayout.Space(guiOffset);
GUILayout.BeginHorizontal();
if (download == null)
{
if (GUILayout.Button("Download " + url))
StartDownload();
}
else
{
if (download.error == null)
{
var progress = parseInt(download.progress * 100);
GUILayout.Label(progress + "%");
if (download.isDone && GUILayout.Button("Unload Resource") )
{
// Destroy the instantiated object
Destroy(instanced);
// Dispose the WWW class
// (This happens automatically from the GC, but you can do it explicitly to make sure it happens early on)
download.Dispose();
download = null;
// Unload the whole asset bundles and any loaded assets
assetBundle.Unload (true);
assetBundle = null;
}
}
else
{
GUILayout.Label(download.error);
}
}
GUILayout.EndHorizontal();
}
预设下载和其他下载差不多,只是预设中多了一步实例化。
项目下载地址:(附送“FF13 - 誓言”)
http://dl.dbank.com/c0038zv73m