Unity3d Assetbundle文件的导出与加载
Assetbundle文件导出
首先在Assets中创建Editor文件夹,添加如下脚本 ExportAssetbundle.cs
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
public class ExportAssetbundle : EditorWindow
{
private static Dictionary<string,string> mNameLs;
//打包FindDirs 函数中 指定文件夹下面的所有 prefab
[MenuItem("Assetbundle/导出一个包 android")]
public static void BuildAndroid()
{
Build("all_ui.local.android.assetbundle", BuildTarget.Android);
}
//打包FindDirs 函数中 指定文件夹下面的所有 prefab
[MenuItem("Assetbundle/导出一个包 web")]
public static void BuildWeb()
{
Build("all_ui.local.webplayer.assetbundle", BuildTarget.WebPlayer);
}
//打包FindDirs 函数中 指定文件夹下面的所有 prefab
[MenuItem("Assetbundle/导出一个包 ios")]
public static void BuildIOS()
{
Build("all_ui.local.ios.assetbundle", BuildTarget.iPhone);
}
//打包选中的某个prefab
[MenuItem("Assetbundle/打包资源")]
public static void BuildSelect()
{
GameObject[] gos = Selection.gameObjects;
foreach (GameObject go in gos)
{
if (go == null)
return;
Debug.Log("--->>" + go.name);
BuildPipeline.BuildAssetBundle(go, null,
Application.streamingAssetsPath + "/../../" + go.name + ".android.assetbundle", BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);
}
foreach (GameObject go in gos)
{
if (go == null)
return;
BuildPipeline.BuildAssetBundle(go, null,
Application.streamingAssetsPath + "/../../" + go.name + ".ios.assetbundle", BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone);
}
foreach (GameObject go in gos)
{
if (go == null)
return;
BuildPipeline.BuildAssetBundle(go, null,
Application.streamingAssetsPath + "/../../" + go.name + ".web.assetbundle", BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneWindows64);
}
}
//查找要导出的prefab文件
private static void FindDirs()
{
mNameLs = new Dictionary<string,string>();
//prefab在项目Assets文件夹中的相对路径
string path = Application.dataPath + "/New prefabs";
string[] info = Directory.GetFiles(path);
foreach (string str in info)
{
if (str.EndsWith(".meta"))
continue;
int index = str.LastIndexOf('\\')+1;
string tem = str.Substring(index, str.Length - index);
mNameLs.Add(tem,"Assets/New prefabs/"+tem);
}
//如果想导出别的相对路径下面的Prefab
//string path2 = Application.dataPath + "/New prefabs2";
//string[] info2 = Directory.GetFiles(path2);
//foreach (string str in info2)
//{
// if (str.EndsWith(".meta"))
// continue;
// int index = str.LastIndexOf('\\') + 1;
// string tem = str.Substring(index, str.Length - index);
// if(!mNameLs.ContainsKey(tem))
// {
// //Debug.Log("tem2===" + tem);
// mNameLs.Add(tem,"Assets/New prefabs2/"+tem);
// }
//}
}
private static void Build(string name,BuildTarget target)
{
FindDirs();
List<Object> mObjs = new List<Object>();
foreach(string key in mNameLs.Keys)
{
Object ob = AssetDatabase.LoadMainAssetAtPath(mNameLs[key]);
if (ob != null)
mObjs.Add(ob);
else
Debug.Log("ERROR not find==" + key);
}
BuildPipeline.BuildAssetBundle(null, mObjs.ToArray(), name, BuildAssetBundleOptions.DeterministicAssetBundle |
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets, target);
}
}
可以看到菜单栏中多了一栏Assetbundle
随便创建两个Prefab用于导出AssetBundle,将创建好的Prefab放在FindDirs函数中写死的New Prefabs文件夹中
然后 Assetbundle -> "导出一个包 web "
我们可以在项目的根目录中找到 all_ui.local.webplayer.assetbundle 文件 ,这个文件就是我们导出的文件 ,里面包含了 两个Prefab 的数据
如果想分别导出两个Prefab 选中某个Prefab(或者多个Prefab), 然后 Assetbundle -> "打包资源"
我们可以在项目的根目录中发现生成了3个对应的文件
Prefab文件名.android.assetbundle
Prefab文件名.ios.assetbundle
Prefab文件名.web.assetbundle
Assetbundle文件加载
关键代码
using UnityEngine;
using System.Collections;
public class assetsmanager : MonoBehaviour {
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/export/";
#else
string.Empty;
#endif
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if (GUILayout.Button("加载所有资源"))
{
//一次加载所有Prefab
StartCoroutine(LoadALLGameObject(PathURL + "all_ui.local.webplayer.assetbundle"));
}
if (GUILayout.Button("加载单个"))
{
//分别加载每个Prefab
StartCoroutine(LoadSingleGameObject(PathURL + "myPrefab.web.assetbundle"));
StartCoroutine(LoadSingleGameObject(PathURL + "myPrefab2.web.assetbundle"));
}
}
//读取全部资源
private IEnumerator LoadALLGameObject(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
//通过Prefab的名称把他们都读取出来
Object obj0 = bundle.assetBundle.Load("myPrefab");
Object obj1 = bundle.assetBundle.Load("myPrefab2");
//加载到游戏中
yield return Instantiate(obj0);
yield return Instantiate(obj1);
bundle.assetBundle.Unload(false);
}
//读取一个资源
private IEnumerator LoadSingleGameObject(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
//加载到游戏中
yield return Instantiate(bundle.assetBundle.mainAsset);
bundle.assetBundle.Unload(false);
}
}