LoadAB伪代码: publicvoidLoadAB(string abName) { if 主包未加载 加载主包及其依赖配置文件 根据传入的abName获取其所有依赖的包名 for 遍历所有依赖的包名 if 遍历到的依赖包名不存在于AB包字典 根据遍历到的包名,加载AB包 将加载的AB包加入到字典 if abName不存在于字典 根据abName,加载AB包 将加载的AB包加入到字典 }
publicvoidLoadAB(string abName) { //加载主包 if (mainAB == null) { mainAB = AssetBundle.LoadFromFile(PathUrl + MainABName); manifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); } //获取依赖包相关信息 AssetBundle ab; //加载主包中的关键配置文件 获取依赖包 string[] strs = manifest.GetAllDependencies(abName); //加载依赖包 for (int i = 0; i < strs.Length; i++) { //判断包是否加载过 if (!abDic.ContainsKey(strs[i])) { ab = AssetBundle.LoadFromFile(PathUrl + strs[i]); abDic.Add(strs[i], ab); } } //加载目标包 if (!abDic.ContainsKey(abName)) { ab = AssetBundle.LoadFromFile(PathUrl + abName); abDic.Add(abName, ab); } }
//同步加载,不指定类型 public Object LoadRes(string abName, string resName) { //加载AB包 LoadAB(abName); Object obj = abDic[abName].LoadAsset(resName); //为了外部使用方便,在加载资源时,先判断是否为Object,如果是直接实例化返回出去 if (obj is GameObject) return Instantiate(obj); else return obj; }
//同步加载,使用type指定类型 public Object LoadRes(string abName, string resName, System.Type type) { //加载AB包 LoadAB(abName); Object obj = abDic[abName].LoadAsset(resName, type); //为了外部使用方便,在加载资源时,先判断是否为Object,如果是直接实例化返回出去 if (obj is GameObject) return Instantiate(obj); else return obj; }
//同步加载,使用泛型指定类型 public T LoadRes<T>(string abName, string resName) where T : Object { //加载AB包 LoadAB(abName); T obj = abDic[abName].LoadAsset<T>(resName); //为了外部使用方便,在加载资源时,先判断是否为Object,如果是直接实例化返回出去 if (obj is GameObject) return Instantiate(obj); else return obj; }
private IEnumerator LoadResCoroutine(string abName, string resName, System.Type type, UnityAction<Object> callBack) { //加载AB包 LoadAB(abName); AssetBundleRequest abr = abDic[abName].LoadAssetAsync(resName, type); yieldreturn abr; //异步加载结束后,通过委托,传递给外部,来使用 if (abr.asset is GameObject) callBack(Instantiate(abr.asset)); else callBack(abr.asset); }
//根据泛型异步加载 publicvoidLoadResAsync<T>(string abName, string resName, UnityAction<T> callBack) where T : Object { StartCoroutine(LoadResCoroutine(abName, resName, callBack)); }
private IEnumerator LoadResCoroutine<T>(string abName, string resName, UnityAction<T> callBack) where T : Object { //加载AB包 LoadAB(abName); AssetBundleRequest abr = abDic[abName].LoadAssetAsync<T>(resName); yieldreturn abr; //异步加载结束后,通过委托,传递给外部,来使用 if (abr.asset is GameObject) callBack(Instantiate(abr.asset as T)); else callBack(abr.asset as T); }
#region 同步加载 publicvoidLoadAB(string abName) { //加载主包 if (mainAB == null) { mainAB = AssetBundle.LoadFromFile(PathUrl + MainABName); manifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); } //获取依赖包相关信息 AssetBundle ab; //加载主包中的关键配置文件 获取依赖包 string[] strs = manifest.GetAllDependencies(abName); //加载依赖包 for (int i = 0; i < strs.Length; i++) { //判断包是否加载过 if (!abDic.ContainsKey(strs[i])) { ab = AssetBundle.LoadFromFile(PathUrl + strs[i]); abDic.Add(strs[i], ab); } } //加载目标包 if (!abDic.ContainsKey(abName)) { ab = AssetBundle.LoadFromFile(PathUrl + abName); abDic.Add(abName, ab); } }
//同步加载,不指定类型 public Object LoadRes(string abName, string resName) { //加载AB包 LoadAB(abName); Object obj = abDic[abName].LoadAsset(resName); //为了外部使用方便,在加载资源时,先判断是否为Object,如果是直接实例化返回出去 if (obj is GameObject) return Instantiate(obj); else return obj; }
//同步加载,使用type指定类型 public Object LoadRes(string abName, string resName, System.Type type) { //加载AB包 LoadAB(abName); Object obj = abDic[abName].LoadAsset(resName, type); //为了外部使用方便,在加载资源时,先判断是否为Object,如果是直接实例化返回出去 if (obj is GameObject) return Instantiate(obj); else return obj; }
//同步加载,使用泛型指定类型 public T LoadRes<T>(string abName, string resName) where T : Object { //加载AB包 LoadAB(abName); T obj = abDic[abName].LoadAsset<T>(resName); //为了外部使用方便,在加载资源时,先判断是否为Object,如果是直接实例化返回出去 if (obj is GameObject) return Instantiate(obj); else return obj; }
private IEnumerator LoadResCoroutine(string abName, string resName, System.Type type, UnityAction<Object> callBack) { //加载AB包 LoadAB(abName); AssetBundleRequest abr = abDic[abName].LoadAssetAsync(resName, type); yieldreturn abr; //异步加载结束后,通过委托,传递给外部,来使用 if (abr.asset is GameObject) callBack(Instantiate(abr.asset)); else callBack(abr.asset); }
//根据泛型异步加载 publicvoidLoadResAsync<T>(string abName, string resName, UnityAction<T> callBack) where T : Object { StartCoroutine(LoadResCoroutine(abName, resName, callBack)); }
private IEnumerator LoadResCoroutine<T>(string abName, string resName, UnityAction<T> callBack) where T : Object { //加载AB包 LoadAB(abName); AssetBundleRequest abr = abDic[abName].LoadAssetAsync<T>(resName); yieldreturn abr; //异步加载结束后,通过委托,传递给外部,来使用 if (abr.asset is GameObject) callBack(Instantiate(abr.asset as T)); else callBack(abr.asset as T); } #endregion