using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Test2:MonoBehaviour {
public static string BundleURL;
private GameObject g;
private string assetname;
private void Awake() {
//平台预处理
BundleURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";//Test_AssetBundle 资源束文件的位置
#elif UNITY_WEBGL
"服务器地址";
#else
string.Empty;
#endif
}
public void OnPrefab() {
StartCoroutine(DownPrefabAndSceneWWW(BundleURL + ".manifest名称"));
}
//WWW加载
IEnumerator DownPrefabAndSceneWWW(string path) {
//电脑端加载使用
using (WWW asset = new WWW(path)) {
yield return asset;
AssetBundle bundle = asset.assetBundle;
//资源加载
//加载的是打包到资源包里面,每个资源的名字
g = Instantiate(bundle.LoadAsset(".manifestPrefab")) as GameObject;
g.transform.position = Vector3.zero;
//资源加载完毕后记得Unload,不然再次加载资源的时候无法加载
bundle.Unload(false);
}
}
//UnityWebRequest加载
IEnumerator DownPrefabAndSceneUnityWebRequest(string path) {
using (UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(url)) {
// Debug.LogError(url);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else//第一种加载方法 需要把场景拖进buildsetting
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
SceneManager.LoadScene(assetname);
Debug.Log(www.GetHashCode());
print("跳转场景");
// AssetBundle.Unload(false),释放AssetBundle文件内存镜像,不销毁Load创建的Assets对象
// AssetBundle.Unload(true),释放AssetBundle文件内存镜像同时销毁所有已经Load的Assets内存镜像
bundle.Unload(false);
}
}
}
}