參數含義: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)); }
(在調用之前要對參數初始化)