using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassABtest:MonoBehaviour{// Start is called before the first frame updatevoidStart(){//加载AB包AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath +"/"+"model");//加载AB包中的资源,不推荐泛型,LUA不能用//GameObject obj = ab.LoadAsset<GameObject>("cube");GameObject obj = ab.LoadAsset("cube",typeof(GameObject))as GameObject;Instantiate(obj);//true会连同场景中已加载的资源一起卸载
ab.Unload(false);}// Update is called once per framevoidUpdate(){}}
异步——协程
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;publicclassABtest2:MonoBehaviour{publicImage img;// Start is called before the first frame updatevoidStart(){StartCoroutine(LoadAB("model","cube"));}// Update is called once per framevoidUpdate(){}IEnumeratorLoadAB(string abName,string resName){AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath +"/"+ abName);yieldreturn abcr;AssetBundleRequest abr = abcr.assetBundle.LoadAssetAsync(resName,typeof(GameObject));yieldreturn abr;GameObject obj = abr.asset as GameObject;Instantiate(obj);}}
依赖
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassABtest:MonoBehaviour{// Start is called before the first frame updatevoidStart(){//加载AB包AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath +"/"+"model");//加载主包AssetBundle abMain = AssetBundle.LoadFromFile(Application.streamingAssetsPath +"/"+"PC");//加载主包中的固定文件AssetBundleManifest abMainifest = abMain.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//从固定文件中得到依赖信息string[] strs = abMainifest.GetAllDependencies("model");//得到依赖包的名字for(int i =0; i < strs.Length; i++){
AssetBundle.LoadFromFile(Application.streamingAssetsPath +"/"+ strs[i]);}//加载AB包中的资源,不推荐泛型,LUA不能用//GameObject obj = ab.LoadAsset<GameObject>("cube");GameObject obj = ab.LoadAsset("cube",typeof(GameObject))as GameObject;Instantiate(obj);//true会连同场景中已加载的资源一起卸载//ab.Unload(false);}// Update is called once per framevoidUpdate(){}}