using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ABAsyncTest : MonoBehaviour
{
void Start()
{
//第一种
ABMgr.GetInstance().LoadResAsync("model", "Cube", (obj) =>
{
(obj as GameObject).transform.position = -Vector3.up;
});
//第二种(Type方式)
ABMgr.GetInstance().LoadResAsync("model", "Cube",typeof(GameObject), (obj) =>
{
(obj as GameObject).transform.position = -Vector3.up;
});
//第三种(泛型方式)
ABMgr.GetInstance().LoadResAsync<GameObject>("model", "Cube", (obj) =>
{
obj.transform.position = -Vector3.up;
});
}
}
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 知识点
/// 1.AB包先关的API
/// 2.单例模式
/// 3.委托 -> Lambda表达式
/// 4.协程
/// 5.字典
/// </summary>
public class ABMgr : SingletonAutoMono<ABMgr>
{
//AB包管理器 目的是:让外部更方便的进行资源加载
//主包
private AssetBundle mainAB = null;
//依赖包获取用的配置文件
private AssetBundleManifest manifest = null;
/// <summary>
/// AB包存放路径 方便修改
/// </summary>
private string pathUrl
{
get
{
return Application.streamingAssetsPath + "/";
}
}
/// <summary>
/// 主包名 方便修改
/// </summary>
private string MainABName
{
get
{
#if UNITY_IOS
return "IOS";
#elif UNITY_ANDROID
return "Android";
#else
return "PC";
}
}
//AB包不能够重复加载 重复加载会报错
//字典 用字典来存储 加载过的AB包
private Dictionary<string, AssetBundle> abDic = new Dictionary<string, AssetBundle>();//存储AB包容器
/// <summary>
/// 加载AB包
/// </summary>
/// <param name="abName"></param>
private void LoadAB(string abName)
{
//加载AB包
if (mainAB == null)
{
mainAB = AssetBundle.LoadFromFile(pathUrl + "PC");
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);
}
}
//异步加载的方法
//这里的异步加载 AB包并没有使用异步加载
//知识从AB包中 加载资源时 使用异步
/// <summary>
/// 根据名字异步加载资源
/// </summary>
/// <param name="abName"></param>
/// <param name="resName"></param>
/// <param name="callBack"></param>
private void LoadResAsync(string abName, string resName,UnityAction<Object> callBack)//提供给外部方法,用于启动协程
{
StartCoroutine(ReallyLoadResAsync(abName,resName,callBack));
}
private IEnumerator ReallyLoadResAsync(string abName,string resName,UnityAction<Object> callBack)//正儿八经要用到的协程
{
AssetBundleRequest abr = abDic[abName].LoadAssetAsync(resName);
//异步加载结束后 通过委托 传递给外部 外部来使用
if(abr.asset == null )
if (abr.asset is GameObject)
{
callBack(Instantiate(abr.asset));
}
else callBack(abr.asset);
yield return null;
}
/// <summary>
/// 根据Type异步加载资源
/// </summary>
/// <param name="abName"></param>
private void LoadResAsync(string abName, string resName, System.Type type,UnityAction<Object> callBack)//提供给外部方法,用于启动协程
{
StartCoroutine(ReallyLoadResAsync(abName, resName, callBack));
}
private IEnumerator ReallyLoadResAsync(string abName, string resName, System.Type type, UnityAction<Object> callBack)//正儿八经要用到的协程
{
AssetBundleRequest abr = abDic[abName].LoadAssetAsync(resName,type);
//异步加载结束后 通过委托 传递给外部 外部来使用
if (abr.asset == null)
if (abr.asset is GameObject)
{
callBack(Instantiate(abr.asset));
}
else callBack(abr.asset);
yield return null;
}
/// <summary>
/// 根据泛型异步加载
/// </summary>
/// <param name="abName"></param>
private void LoadResAsync<T>(string abName, string resName, UnityAction<T> callBack) where T:Object//提供给外部方法,用于启动协程
{
StartCoroutine(ReallyLoadResAsync<T>(abName, resName, callBack));
}
private IEnumerator ReallyLoadResAsync<T>(string abName, string resName, UnityAction<T> callBack)//正儿八经要用到的协程
{
AssetBundleRequest abr = abDic[abName].LoadAssetAsync(resName);
//异步加载结束后 通过委托 传递给外部 外部来使用
if (abr.asset == null)
if (abr.asset is GameObject)
{
callBack(Instantiate(abr.asset) as T);
}
else callBack(abr.asset);
yield return null;
}
//单个包卸载
public void UnLoad(string abName)
{
if (abDic.ContainsKey(abName))
{
abDic[abName].Unload(false);
abDic.Remove(abName);
}
}
//所有包的卸载
public void ClearAB()
{
AssetBundle.UnloadAllAssetBundles(false);
abDic.Clear();
mainAB = null;
manifest = null;
}
}