加载AB包注意事项
1.基础的正确的加载方式
2.有问题的基础的加载方式
3.总结 :在加载AB包资源的时候 必须先加载资源所依赖的资源 不然可能会造成贴图或者其他成分丢失 这个值得注意
Manifest基础解释
- Dependencies :当前资源所依赖的资源
- Assets:当前资源的路径
从内存中加载AB包
//2.从内存加载AB包
string path2 = "C:/Users/Administrator/Desktop/WorkSpace5.6/AB_Packet/Assets/AssetBundles/cubewall";
//异步返回的是一个请求Request
AssetBundleCreateRequest request1 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
yield return request2;
//同步方式直接返回的是AssetBundle
AssetBundle.LoadFromMemory(File.ReadAllBytes(path2));
AssetBundle ab = request2.assetBundle;
GameObject cube = (ab.LoadAsset("Cube")) as GameObject;
Instantiate(cube);
从服务器加载AB包
//3.从服务器种加载AB包的方式www
IEnumerator Start()
{
//string path2 = "C:/Users/Administrator/Desktop/WorkSpace5.6/AB_Packet/Assets/AssetBundles/cubewall";
string path2 = @"http://localhost/AssetBundles/cubewall";
WWW www = WWW.LoadFromCacheOrDownload(path2, 1);
while (Caching.ready == false)
{
yield return null;
}
yield return www;
if (string.IsNullOrEmpty(www.error) == false)
{
Debug.Log(www.error);
yield break;
}
AssetBundle ab = www.assetBundle;
//使用
GameObject Cube = ab.LoadAsset<GameObject>("Cube");
Instantiate(Cube);
}
通过WebRequest加载
//第4种方式通过WebRequest
IEnumerator Start()
{
string uri = @"http://localhost/AssetBundles/cubewall";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
yield return request.Send();
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
DownloadHandlerAssetBundle ab = request.downloadHandler as DownloadHandlerAssetBundle;
GameObject Cube = ab.assetBundle.LoadAsset<GameObject>("Cube");
Instantiate(Cube);
//通过加载Mainifest文件来加载资源所依赖的包
AssetBundle assetBundle = AssetBundle.LoadFromFile(@"C:\Users\Administrator\Desktop\WorkSpace5.6\AB_Packet\Assets\AssetBundles\AssetBundles");
AssetBundleManifest abManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
foreach (string name in abManifest.GetAllDependencies("cubewall"))
{
print(name);
AssetBundle.LoadFromFile(@"C:\Users\Administrator\Desktop\WorkSpace5.6\AB_Packet\Assets\AssetBundles\" + name);
}