AssetBundle的四种加载方式

//生成ab包的方法
void CreatAssetBundles()
{

    string dir = "AssetBundles";//创建一个路径的对象
    if(Directory.Exists(dir)==false)//如果这个路径为空
    {    
        Directory.CreateDiretory(dir);//创建这个路径

    }
      
    BuildPipeline.BuildAssetBundles//生成ab包的方法
    (dir,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);

    //BuildAssetBundleOptions.UncompressedAssetBundle不压缩
    //BuildAssetBundleOptions.None 默认的压缩方式LZMA,压缩率高,加载慢
    //并且加载的时候必须全部加载,但是解压完成后会重新以LZ4重新压缩
    //BuildAssetBundleOptions.ChunkBasedCompression是LZ4压缩算法
    //压缩率中等,但是加载的时候可以不用全部解压,用那一部分的数据解压那一部分


}



//加载ab包的方法--------------------------------------------------------------------------
//同步加载为普通方法     异步加载为协程




//本地同步加载ab包...............................................................1
string path = "AssetBundles/cube.ab";//创建要加载的路径的对象
AssetBundle ab = AssetBundle.LoadFromFile(path);//从这个路径加载到这个ab包
GameObject cubePrefab = ab.loadAsset<GameObject>("cube");//获取预设物  cube必须为预设物真名
Instantiate(cubePrefab);实例化对象

//从本地加载多个ab包依赖的贴图ab包.................................................2
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/share.ab");
Object[] objs = ab.LoadAllAssets();//获取所有的预设物对象
foreach (Object o in objs)
{
//遍历所有预设物然后实例化
Instantiate(o);
}

//从本地异步加载ab包...............................................................3
//获取异步加载的对象
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
yield return request;//异步加载
AssetBundle ab = request.assetBundle;//获取正在加载的ab包的对象
GameObject cubePrefab = ab.loadAsset<GameObject>("cube");
Instantiate(cubePrefab);


//从内存异步加载ab包................................................................4
AssetBundleCteateRequest request = AssetBundle.LoadFromMemoryAsync
(File.ReadAllBytes(path));//从内存流当中获取到异步加载的对象
yield return request;//异步加载
AssetBundle ab =request.assetBundle;
GameObject cubePrefab = ab.loadAsset<GameObject>("cube");
Instantiate(cubePrefab);

//从内存中本地加载ab包..............................................................5
AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
GameObject cubePrefab = ab.loadAsset<GameObject>("cube");
Instantiate(cubePrefab);


//从网络加载ab包www...................................................................6
while (Caching.ready==false)
{
//当缓存成功的时候执行下面代码,否则还没有缓存成功就执行会报错
yield return null;
}

WWW www = WWW.LoadFromCacheOrDownload@"http://localhost/AssetBundles/cubewall.unity3d", 1);//路径和版本号
yield return www;//从网络中加载ab包

 //WWW.error:如果下载过程中出现错误,则返回错误消息(只读)。
 if (string.IsNullOrEmpty(www.error) == false)
{
            Debug.Log(www.error);
             yield break;
}

 AssetBundle ab = www.assetBundle;//获取加载到的ab包
GameObject cubePrefab = ab.loadAsset<GameObject>("cube");
Instantiate(cubePrefab);


//从网络加载ab包unity...............................................................7

 string uri = @"http://localhost/AssetBundles/cubewall.unity3d";//创建路径
 UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);//创建UnityWebRequest对象
 yield return request.Send();//通过调用Send方法与远程服务器通信

//DownloadHandlerAssetBundle一个专门用于下载AssetBundle的DownloadHandler子类
//GetContent:返回下载的AssetBundle,或null。

AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

GameObject cubePrefab = ab.loadAsset<GameObject>("cube");
Instantiate(cubePrefab);


//从给定的ab包中加载所需的依赖项
//获取ab包
 AssetBundle manifestAB =  AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
//获取这个ab包中AssetBundleManifest文件
 AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//通过这个文件找到所有的关于给定ab包中的依赖项
 string[] strs =  manifest.GetAllDependencies("cubewall.unity3d");
////GetAllDependencies:获取给定AssetBundle的所有相关依赖的AssetBundle
 foreach (string name in strs)
  {
        print(name);
       AssetBundle.LoadFromFile("AssetBundles/" + name);//加载给定AssetBundle中的所有依赖项
  }
















































猜你喜欢

转载自blog.csdn.net/qq_41764460/article/details/81396616
今日推荐