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));
        }
 
 

(在调用之前要对参数初始化)

 

猜你喜欢

转载自www.cnblogs.com/blackteeth/p/10152121.html