Unity中Android加载assetbundle需要注意的两点问题

Posted Fei非非

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity中Android加载assetbundle需要注意的两点问题相关的知识,希望对你有一定的参考价值。

1、打包需要用android的路径打包

 

 

using UnityEngine;
using System.Collections;
using UnityEditor;

public class Test : Editor
{

[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain()
{
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

//遍历所有的游戏对象
foreach (Object obj in SelectedAsset)
{
//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//StreamingAssets是只读路径,不能写入
//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
//BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))
{
Debug.Log(obj.name + "资源打包成功");
}
else
{
Debug.Log(obj.name + "资源打包失败");
}
}
//刷新编辑器
AssetDatabase.Refresh();

}

[MenuItem("Custom Editor/Create AssetBunldes ALL")]
static void CreateAssetBunldesALL()
{

Caching.CleanCache();


string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";


Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

foreach (Object obj in SelectedAsset)
{
Debug.Log("Create AssetBunldes name :" + obj);
}

//这里注意第二个参数就行
if (BuildPipeline.BuildAssetBundle(null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies))
{
AssetDatabase.Refresh();
}
else
{

}
}

[MenuItem("Custom Editor/Create Scene")]
static void CreateSceneALL()
{
//清空一下缓存
Caching.CleanCache();
string Path = Application.dataPath + "/MyScene.unity3d";
string[] levels = { "Assets/Level.unity" };
//打包场景
BuildPipeline.BuildPlayer(levels, Path, BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh();
}

}

 

 

 

2、加载也需要用Android的路径加载

 

using UnityEngine;
using System.Collections;
using System.IO;

public class RunScript : MonoBehaviour
{
string DownPath = "";

////不同平台下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 + "/StreamingAssets/";
//#else
// string.Empty;
//#endif

void OnGUI()
{
if (GUI.Button(new Rect(50, 100, 100, 100), "Main Assetbundle"))
{
StartCoroutine(LoadMainGameObject("http://10.131.229.166/MyItems/Cube.assetbundle"));
StartCoroutine(LoadMainGameObject("jar:file://" + Application.dataPath + "!/assets/" + "Cube.assetbundle"));

}
}
//读取一个资源

private IEnumerator LoadMainGameObject(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
//加载到游戏中
yield return Instantiate(bundle.assetBundle.mainAsset);
Debug.Log("+++++++++++++++++++++++++++Load successful!!");
bundle.assetBundle.Unload(false);
}

//从服务器下载资源
private IEnumerator DownLoadToLocal(string url)
{
WWW.EscapeURL(url); //url编码
WWW www = new WWW(url);//访问url
WWW.UnEscapeURL(url); //url解码
string filename = url.Substring(url.LastIndexOf(\'/\') + 1); //根据URL获取文件的名字。
yield return www; //等待下载
if (www.error == null)
{

FileStream fs = File.Create(Application.persistentDataPath + "/" + filename); //path为你想保存文件的路径。
DownPath = Application.persistentDataPath + "/" + filename; //把下载的数据写到文件
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
Debug.Log(filename + "下载完成");

}
else
{
Debug.Log(www.error);
}
}

}

 

 

文件夹的名字也不要弄错了。

 

以上是关于Unity中Android加载assetbundle需要注意的两点问题的主要内容,如果未能解决你的问题,请参考以下文章

unity5本地文件assetbundle怎么加载

Unity3d 5.x AssetBundle打包与加载

AssetBundle一些问题

unity 发布webgl怎么加载assetbundle

unity 是怎么加载assetbundle

unity创建和加载AssetBundle