AssetBoundle加载非预设资源
程序员文章站
2022-07-05 23:50:31
参数含义:ABURL:要下载的AB包地址 go:用于测试,显示加载贴图 assetName:要加载的资源名称 (在调用之前要对参数初始化) ......
1.定义一个协程loadnonobjfromab
ienumerator loadnonobjfromab(string aburl, gameobject go, string assetname)
参数含义:aburl:要下载的ab包地址 go:用于测试,显示加载贴图 assetname:要加载的资源名称
ienumerator loadnonobjfromab(string aburl, gameobject go, string assetname) { //参数检查 if(string.isnullorempty(aburl) || go == null) { debug.logerror("参数错误!"); } using (www www = new www(aburl)) { yield return www; assetbundle ab = www.assetbundle; //获取ab包 if(ab != null) { if(assetname == "") { go.getcomponent<renderer>().material.maintexture = ab.mainasset as texture; } else { go.getcomponent<renderer>().material.maintexture = (texture)ab.loadasset(assetname); //替换贴图为下载的贴图 print(assetname); } //卸载ab包 ab.unload(false); } else { debug.logerror("下载错误:"+www.error); } } }
2.调用协程
private void start() { startcoroutine(loadnonobjfromab(url1, testgo, assetname1)); }
(在调用之前要对参数初始化)