[Unity热更新]unity5中的assetbundle
2015-11-13 13:54
711 查看
参考链接:
http://liweizhaolili.blog.163.com/blog/static/16230744201541410275298/ http://liweizhaolili.blog.163.com/blog/static/162307442015282017852/ http://itfish.net/article/35172.html
简单的说,unity打包时,会为每个文件创建一个.unity3d文件和.manifest文件。其中.unity3d文件包含了被打包的资源,.manifest文件记录该资源的依赖关系。然后所有.manifest文件汇总到一个总的依赖文件中,在游戏最开始运行的时候需要加载这个总的依赖文件,然后加载assetbundle的时候,从中获取到依赖关系来加载依赖。在操作中,实际操作的还是.unity3d文件,而.manifest文件主要的作用就是查看依赖关系。
总结起来,就是四步:
1.为assetbundle起名字,并打包
2.加载总的依赖文件
3.加载assetbundle的依赖文件
4.加载assetbundle
1.命名并打包
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class CreateAssetBundle : Editor {
[MenuItem("Tool/SetAssetBundleNameExtension")]
static void SetBundleName()
{
#region 设置资源的AssetBundle的名称和文件扩展名
UnityEngine.Object[] selects = Selection.objects;
foreach (UnityEngine.Object selected in selects)
{
string path = AssetDatabase.GetAssetPath(selected);
AssetImporter asset = AssetImporter.GetAtPath(path);
asset.assetBundleName = selected.name; //设置Bundle文件的名称
asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名
asset.SaveAndReimport();
}
AssetDatabase.Refresh();
#endregion
}
[MenuItem("Tool/BuildAll")]
static void Build()
{
string path = Application.dataPath + "/AB";
if(!Directory.Exists(path)) Directory.CreateDirectory(path);
BuildPipeline.BuildAssetBundles(path);
AssetDatabase.Refresh();
}
}
或者也可以这样:
2.加载
using UnityEngine;
using System.Collections;
public class AssetbBundleManager : MonoSingletion<AssetbBundleManager> {
public delegate void OnLoadAssetbBundleFinish(AssetBundle assetBundle, string nameWithoutExtension);
/// <summary>
///
/// </summary>
/// <param name="assetBundleDir">Assetbundle的文件夹 </param>
/// <param name="assetBundleName">要加载的AssetBundle的名字</param>
public void Load(string assetBundleDir, string assetBundleName, OnLoadAssetbBundleFinish onLoadFinish)
{
StartCoroutine(LoadIEnumerator(assetBundleDir, assetBundleName, onLoadFinish));
}
private IEnumerator LoadIEnumerator(string assetBundleDir, string assetBundleName, OnLoadAssetbBundleFinish onLoadFinish)
{
//首先加载总Manifest文件;
WWW wwwAll = new WWW(assetBundleDir + "AB");
yield return wwwAll;
if (!string.IsNullOrEmpty(wwwAll.error))
{
Debug.LogError(wwwAll.error);
}
else
{
AssetBundle ab = wwwAll.assetBundle;
AssetBundleManifest manifest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest");
ab.Unload(false);
//获取依赖文件列表;
string[] depends = manifest.GetAllDependencies(assetBundleName);
AssetBundle[] dependsAssetBundle = new AssetBundle[depends.Length];
for (int index = 0; index < depends.Length; index++)
{
//加载所有的依赖文件;
WWW dwww = WWW.LoadFromCacheOrDownload(assetBundleDir + depends[index], 0);
yield return dwww;
dependsAssetBundle[index] = dwww.assetBundle;
}
//加载我们需要的文件;
WWW www = new WWW(assetBundleDir + assetBundleName);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogError(www.error);
}
else
{
string name = assetBundleName.Substring(0, assetBundleName.IndexOf("."));
AssetBundle assetBundle = www.assetBundle;
onLoadFinish(assetBundle, name);
assetBundle.Unload(false);
}
//卸载依赖文件的包
for (int index = 0; index < depends.Length; index++)
{
dependsAssetBundle[index].Unload(false);
}
}
}
public string GetString(AssetBundle assetBundle, string name)
{
return assetBundle.LoadAsset<TextAsset>(name).ToString();
}
public GameObject GetGameObject(AssetBundle assetBundle, string name)
{
return assetBundle.LoadAsset<GameObject>(name);
}
}
http://liweizhaolili.blog.163.com/blog/static/16230744201541410275298/ http://liweizhaolili.blog.163.com/blog/static/162307442015282017852/ http://itfish.net/article/35172.html
简单的说,unity打包时,会为每个文件创建一个.unity3d文件和.manifest文件。其中.unity3d文件包含了被打包的资源,.manifest文件记录该资源的依赖关系。然后所有.manifest文件汇总到一个总的依赖文件中,在游戏最开始运行的时候需要加载这个总的依赖文件,然后加载assetbundle的时候,从中获取到依赖关系来加载依赖。在操作中,实际操作的还是.unity3d文件,而.manifest文件主要的作用就是查看依赖关系。
总结起来,就是四步:
1.为assetbundle起名字,并打包
2.加载总的依赖文件
3.加载assetbundle的依赖文件
4.加载assetbundle
1.命名并打包
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class CreateAssetBundle : Editor {
[MenuItem("Tool/SetAssetBundleNameExtension")]
static void SetBundleName()
{
#region 设置资源的AssetBundle的名称和文件扩展名
UnityEngine.Object[] selects = Selection.objects;
foreach (UnityEngine.Object selected in selects)
{
string path = AssetDatabase.GetAssetPath(selected);
AssetImporter asset = AssetImporter.GetAtPath(path);
asset.assetBundleName = selected.name; //设置Bundle文件的名称
asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名
asset.SaveAndReimport();
}
AssetDatabase.Refresh();
#endregion
}
[MenuItem("Tool/BuildAll")]
static void Build()
{
string path = Application.dataPath + "/AB";
if(!Directory.Exists(path)) Directory.CreateDirectory(path);
BuildPipeline.BuildAssetBundles(path);
AssetDatabase.Refresh();
}
}
或者也可以这样:
using UnityEngine; using System.Collections; using UnityEditor; public class AutoSetAssetBundleName : AssetPostprocessor { static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (var str in importedAssets) { //Debug.Log("Reimported Asset: " + str); if(!str.EndsWith(".cs")) { AssetImporter importer = AssetImporter.GetAtPath(str); importer.assetBundleName = str; } } foreach (var str in deletedAssets) { //Debug.Log("Deleted Asset: " + str); if(!str.EndsWith(".cs")) { AssetImporter importer = AssetImporter.GetAtPath(str); importer.assetBundleName = str; } } for (var i=0; i<movedAssets.Length; i++) { //Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]); } } }
2.加载
using UnityEngine;
using System.Collections;
public class AssetbBundleManager : MonoSingletion<AssetbBundleManager> {
public delegate void OnLoadAssetbBundleFinish(AssetBundle assetBundle, string nameWithoutExtension);
/// <summary>
///
/// </summary>
/// <param name="assetBundleDir">Assetbundle的文件夹 </param>
/// <param name="assetBundleName">要加载的AssetBundle的名字</param>
public void Load(string assetBundleDir, string assetBundleName, OnLoadAssetbBundleFinish onLoadFinish)
{
StartCoroutine(LoadIEnumerator(assetBundleDir, assetBundleName, onLoadFinish));
}
private IEnumerator LoadIEnumerator(string assetBundleDir, string assetBundleName, OnLoadAssetbBundleFinish onLoadFinish)
{
//首先加载总Manifest文件;
WWW wwwAll = new WWW(assetBundleDir + "AB");
yield return wwwAll;
if (!string.IsNullOrEmpty(wwwAll.error))
{
Debug.LogError(wwwAll.error);
}
else
{
AssetBundle ab = wwwAll.assetBundle;
AssetBundleManifest manifest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest");
ab.Unload(false);
//获取依赖文件列表;
string[] depends = manifest.GetAllDependencies(assetBundleName);
AssetBundle[] dependsAssetBundle = new AssetBundle[depends.Length];
for (int index = 0; index < depends.Length; index++)
{
//加载所有的依赖文件;
WWW dwww = WWW.LoadFromCacheOrDownload(assetBundleDir + depends[index], 0);
yield return dwww;
dependsAssetBundle[index] = dwww.assetBundle;
}
//加载我们需要的文件;
WWW www = new WWW(assetBundleDir + assetBundleName);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogError(www.error);
}
else
{
string name = assetBundleName.Substring(0, assetBundleName.IndexOf("."));
AssetBundle assetBundle = www.assetBundle;
onLoadFinish(assetBundle, name);
assetBundle.Unload(false);
}
//卸载依赖文件的包
for (int index = 0; index < depends.Length; index++)
{
dependsAssetBundle[index].Unload(false);
}
}
}
public string GetString(AssetBundle assetBundle, string name)
{
return assetBundle.LoadAsset<TextAsset>(name).ToString();
}
public GameObject GetGameObject(AssetBundle assetBundle, string name)
{
return assetBundle.LoadAsset<GameObject>(name);
}
}
using UnityEngine; using System.Collections; using LuaInterface; public class LoadAssetbBundle : MonoBehaviour { void Start() { string dir = @"file:///" + Application.persistentDataPath + "/"; //Debug.Log(dir); AssetbBundleManager.Instance.Load(dir, "newuluascript.unity3d", (ab, name) => { string content = AssetbBundleManager.Instance.GetString(ab, name); LuaScriptMgr lua = new LuaScriptMgr(); lua.Start(); lua.DoString(@content); }); } }
相关文章推荐
- Unity5的AssetBundle与NGUI的坑
- 在Unity中创建可远程加载的.unity3d包
- 资源打包Assetbundle
- Unity3d发布Android版通过WWW加载本地AssetBundle资源
- Unity 资源打包Assetbundle
- unity5.X AssetBundle使用(二 )
- Unity3D之AssetBundle【1】AssetBundle流程及原理
- Unity3D之AssetBundle【2】AssetBundle流程分析
- Unity3D之AssetBundle【3】AssetBundle资源加载管理器
- Unity3D之AssetBundle【4】简易AssetBundle打包、加载demo
- Unity3D之AssetBundle【5】打包场景
- Unity3D之AssetBundle【6】解包场景
- Unity3D之AssetBundle【7】共享资源打包/依赖资源打包
- Unity3D之手游开发中特殊的文件夹
- AssetBundle机制相关资料收集
- Unity中资源打包成Assetsbundle的资料整理
- Unity3D研究院之Assetbundle的使用
- 细说Unity3D(一)——移动平台动态读取外部文件全解析
- Unity3D 自动打包整个项目(以AssetBundle实现)
- <Unity> AssetBundle