AssetBundle的四种加载方式
程序员文章站
2022-05-15 18:38:00
...
//生成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 = [email protected]"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中的所有依赖项
}
上一篇: 爬虫第二天--美女图片
下一篇: CSS的四种引入方式
推荐阅读
-
BI报告:最具市场潜力的四种移动支付方式
-
ASP.NET MVC下的四种验证编程方式[续篇]
-
JAVA发送HTTP请求的四种方式总结
-
js前端实现图片懒加载(lazyload)的两种方式
-
.net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式
-
Vue加载组件、动态加载组件的几种方式
-
Javascript中绑定click事件的四种方式介绍
-
详解bootstrap的modal-remote两种加载方式【强化】
-
尝试加载Oracle客户端库时引发BadImageFormatException的问题记录和解决方式
-
PHP URL参数获取方式的四种例子