AssetBoundle加载非预设资源
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)); }
(在调用之前要对参数初始化)
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步