unity从服务器上下载AssertBundle,放在手机中,在游戏中加载
游戏中很多资源不需要放在安装包中,这个可以增加游戏的加载速度,并且让游戏更加流畅。
这里提供一个网上提供的方法,都是整合大牛的代码,在这里感谢他们的分享。
首先制作AssertBundle,这是一个简单制作的方法https://blog.****.net/qq_33515628/article/details/80466361
下一步是从服务器上面下载到本地,首先是把AssertBundle放在服务器上面,这里是下载方法,其中holdPath使用的是Application.persistentDataPath + "/anim_main_bg.assetbundle",Application.persistentDataPath 无论在手机中还是在编辑器上面都是可以读可以写的。
IEnumerator DownFile(string url)
{
Debug.Log("开始下载");
WWW www = new WWW(url);
while (!www.isDone)
{
string LoadPro = (((int)(www.progress * 100)) % 100) + "%";
Debug.Log("进度:" + LoadPro);
yield return 1;
}
yield return www;
if (www.isDone)
{
Debug.Log("下载完成");
byte[] bytes = www.bytes;
CreatFile(bytes);
//this.onInstallAPK();
//yield return 1;
}
}
void CreatFile(byte[] bytes)
{
Stream stream;
//text_test.GetComponent<Text>().text = holdPath;
Debug.Log("下载地址" + holdPath);
FileInfo file1 = new FileInfo(holdPath);
stream = file1.Create();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
}
因为不同的设备上面,Application.persistentDataPath的路径不同,这里是不同路径的方法:
m_PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/QAB/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/QAB/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR //这里必须要加 "file:///" ,不然路径会报错
"file:///" + Application.persistentDataPath;
#else
string.Empty;
#endif
之后就是展示出来了,使用www方法下载
WWW bundle = new WWW(path);
yield return bundle;
AssetBundle ab = bundle.assetBundle;
GameObject go = ab.LoadAsset("bg") as GameObject;
GameObject go1 = Instantiate(go);
go1.transform.SetParent(this.transform);
下面是展示出来所有的代码
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class TestAssertDownLand : MonoBehaviour
{
public string m_PathURL;
private string holdPath;
// Use this for initialization
void Start()
{
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
holdPath = Application.persistentDataPath + "/anim_main_bg.assetbundle";
m_PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/QAB/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/QAB/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
@"file://" + Application.dataPath + "/AssetBundleLearn/StreamingAssets/";
#else
string.Empty;
#endif
}
public void onClick()
{
Debug.Log("开始下载");
StartCoroutine(DownFile("服务器下载地址"));
}
//单独读取资源
private IEnumerator LoadGameObjectPackedByThemselves(string path)
{
//这里编辑器测试使用,如果打包测试,需要注释掉
path = "file:///" + Application.persistentDataPath + "/anim_main_bg.assetbundle";
WWW bundle = new WWW(path);
yield return bundle;
AssetBundle ab = bundle.assetBundle;
GameObject go = ab.LoadAsset("bg") as GameObject;
GameObject go1 = Instantiate(go);
go1.transform.SetParent(this.transform);
//go1.GetComponent<Image>().SetNativeSize();
go1.transform.localScale = new Vector3(1, 1, 1);
go1.transform.localPosition = Vector3.zero;
go1.GetComponent<RectTransform>().sizeDelta = new Vector2(0, 0);
bundle.assetBundle.Unload(false);
yield return new WaitForSeconds(5);
}
IEnumerator DownFile(string url)
{
Debug.Log("开始下载");
WWW www = new WWW(url);
while (!www.isDone)
{
string LoadPro = (((int)(www.progress * 100)) % 100) + "%";
Debug.Log("进度:" + LoadPro);
yield return 1;
}
yield return www;
if (www.isDone)
{
Debug.Log("下载完成");
byte[] bytes = www.bytes;
CreatFile(bytes);
}
}
void CreatFile(byte[] bytes)
{
Stream stream;
Debug.Log("下载地址" + holdPath);
FileInfo file1 = new FileInfo(holdPath);
stream = file1.Create();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
}
public void Onclick1()
{
StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL + "anim_main_bg.assetbundle"));
}
public IEnumerator LoadXML()
{
string sPath = "file://" + Application.persistentDataPath + "/anim_main_bg.assetbundle";
WWW www = new WWW(sPath);
while (!www.isDone)
{
string LoadPro = (((int)(www.progress * 100)) % 100) + "%";
Debug.Log("进度:" + LoadPro);
yield return 1;
}
yield return www;
if (www.isDone)
{
GameObject go = www.assetBundle.LoadAsset("bg") as GameObject;
go.transform.SetParent(this.transform);
}
}
IEnumerator LoadGameObjectPackedTogether(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
//Object one = bundle.assetBundle.Load("One");
//Object two = bundle.assetBundle.Load("Two");
//Object three = bundle.assetBundle.Load("Three");
//加载
//yield return Instantiate(one);
//yield return Instantiate(two);
//yield return Instantiate(three);
bundle.assetBundle.Unload(false);
}
IEnumerator loadAndSaveAsset(string url) //下载网络模型并缓存到本地
{
Debug.Log("下载网络模型并缓存到本地 ");
WWW w = new WWW(url);
yield return w;
if (w.isDone)
{
byte[] model = w.bytes;
int length = model.Length;
//文件流信息
//StreamWriter sw;
Stream sw;
FileInfo t = new FileInfo(Application.persistentDataPath + "//" + "anim_main_bg.assetbundle");
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.Create();
}
else
{
//如果此文件存在则打开
sw = t.OpenWrite();
}
sw.Write(model, 0, length);
byte[] bytes = new byte[sw.Length];
sw.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
sw.Seek(0, SeekOrigin.Begin);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
}
}
上面的代码onClick和onClick1分别是下载assertBundel,一个是展示assertBundle中的内容
显示的效果: