/// <summary>
/// 本地同步加载
/// </summary>
void Synchronous(string ObjName)
{
string path = "AssetBundles/"+ObjName+".unity3d";
AssetBundle ab = AssetBundle.LoadFromFile(path);
GameObject tmp = ab.LoadAsset<GameObject>(ObjName);
GameObject go = Instantiate(tmp);
go.transform.parent = this.transform;
go.transform.localPosition = Vector3.zero;
//加载所有ab包
// //object []all= ab.LoadAllAssets();
// // foreach (var o in all)
// // {
// // Instantiate(o as GameObject);
// // }
}
/// <summary>
/// 第一种加载,www加载
/// </summary>
/// <returns></returns>
IEnumerator Www(string ObjName)
{
string path = "AssetBundles/" + ObjName + ".unity3d";
AssetBundleCreateRequest re = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
yield return re;
AssetBundle ab = re.assetBundle;
GameObject tmp = ab.LoadAsset<GameObject>(ObjName);
GameObject go= Instantiate(tmp);
go.transform.parent = this.transform;
go.transform.localPosition = Vector3.zero;
}
IEnumerator WwW(string ObjName)
{
// string path = @"E:\UnityWork\MyAssetBundle\AssetBundles\" + ObjName + ".unity3d";
string path = @"http://localhost/AssetBundles/" + ObjName + ".unity3d";
while (!Caching.ready)
{
yield return null;
}
WWW www = WWW.LoadFromCacheOrDownload(path,5);
yield return www;
if (www.error!=null)
{
Debug.Log(www.error);
yield break;
}
AssetBundle ab = www.assetBundle;
GameObject tmp = ab.LoadAsset<GameObject>(ObjName);
GameObject go = Instantiate(tmp);
go.transform.parent = this.transform;
go.transform.localPosition = Vector3.zero;
}
/// <summary>
/// 第四种UnitywbeRequest
/// </summary>
/// <returns></returns>
IEnumerator UnityWbe(string ObjName)
{
string uri = @"http://localhost/AssetBundles/" + ObjName + ".unity3d";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
yield return request.SendWebRequest();
// AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);//加载多种
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
// File.WriteAllBytes(); //保存
GameObject tmp = ab.LoadAsset<GameObject>(ObjName);
GameObject go = Instantiate(tmp);
go.transform.parent = this.transform;
go.transform.localPosition = Vector3.zero;
}
/// <summary>
/// 第二种方式,内存同步加载
/// </summary>
void Memory(string ObjName)
{
string path = "AssetBundles/" + ObjName + ".unity3d";
AssetBundle ab= AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
GameObject tmp = ab.LoadAsset<GameObject>(ObjName);
GameObject go = Instantiate(tmp);
go.transform.parent = this.transform;
go.transform.localPosition = Vector3.zero;
}