using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class aball : MonoBehaviour {
// Use this for initialization
//void Start () {
// //从本地加载资源 LoadFromFile
// //从ab所在的路径中获取打包的资源
// AssetBundle ab = AssetBundle.LoadFromFile("AssetBudlls/par.unity");
// //如果材质贴图单独打包的话,需要先加载材质贴图资源。打包出来的manifest文件中Assets是包里资源的位置名字,Dependencies是这个包所依赖的别的资源。
// AssetBundle abb = AssetBundle.LoadFromFile("AssetBudlls/mat.unity");
// //实例化资源 里面写之前打包的资源的名字
// GameObject wa = ab.LoadAsset<GameObject>("Cube");
// Instantiate(wa);
// //获取到打包的所有资源
// Object[] ob = ab.LoadAllAssets();
// foreach(Object o in ob)
// {
// Instantiate(o);
// }
//}
//本地加载异步 LoadFromFileAsync
//private IEnumerator Start()
//{
// string st = "AssetBudlls/par.unity";
// AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(st);
// yield return request;//等待异步加载完成
// AssetBundle ab = request.assetBundle;
// //使用
// GameObject wa = ab.LoadAsset<GameObject>("Cube");
// Instantiate(wa);
//}
//异步加载资源 LoadFromMemoryAsync
//private IEnumerator Start()
//{
// string st = "AssetBudlls/par.unity";
// AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(st));
// yield return request;//等待异步加载完成
// AssetBundle ab = request.assetBundle;
// //使用
// GameObject wa = ab.LoadAsset<GameObject>("Cube");
// Instantiate(wa);
//}
//同步加载 LoadFromMemory
//private void Start()
//{
// string st = "AssetBudlls/par.unity";
// AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(st));
// //使用
// GameObject wa = ab.LoadAsset<GameObject>("Cube");
// Instantiate(wa);
//}
//www本地加载
//private IEnumerator Start()
//{
// while (Caching.ready == false)
// {
// yield return null;
// }
// //本地路径要加@ file//或file///
// // WWW www = WWW.LoadFromCacheOrDownload(@"file:///F:\unityproject\bundle\AssetBudlls\par.unity",1);
// //从服务器加载
// WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost:1469/AssetBudlls\par.unity", 1);
// yield return www;//等待加载完成
// if (string.IsNullOrEmpty(www.error)==false)//如果www的错误不是空
// {
// Debug.Log(www.error);
// yield break;//中断协程
// }
// AssetBundle ab = www.assetBundle;
// GameObject wa = ab.LoadAsset<GameObject>("Cube");
// Instantiate(wa);
//}
//
private IEnumerator Start()
{
string uri = @"file:///F:\unityproject\bundle\AssetBudlls\par.unity";//网址或本地
UnityWebRequest request= UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return request.Send();
//第一种
// AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//第二种
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
GameObject wa = ab.LoadAsset<GameObject>("Cube");
Instantiate(wa);
//获取AssetBundleManiifest内容
AssetBundle manifestab = AssetBundle.LoadFromFile("AssetBudlls/AssetBudlls");
AssetBundleManifest manifest = manifestab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//获取打包的名字
foreach(string name in manifest.GetAllAssetBundles())
{
print(name);
}
//获取依赖的包
string[] strs= manifest.GetAllDependencies("par.unity");
foreach(string name in strs)
{
print(name);
//加载所依赖的包
AssetBundle.LoadFromFile("AssetBudlls/" + name);
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
//为true时 会卸载所有资源 即使被使用着 为false时只会卸载没有使用的资源
//使用false时留下来的资源可以通过Resources.UnloadUnusedAssets或者在场景切换的时候自动卸载
AssetBundle.UnloadAllAssetBundles(false);
}
}
}