123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class LoadModelAB : MonoBehaviour
- {
- public List<string> strlist;
- public GameObject loadtext;
- public static Dictionary<string,GameObject> ModelDic = new Dictionary<string, GameObject>();
- // Start is called before the first frame update
- void Start()
- {
- for (int i = 0;i<strlist.Count;i++)
- {
- StartCoroutine(LoadAB(strlist[i]));
- }
- }
- IEnumerator LoadAB(string assetBundleName)
- {
- AssetBundle ab =null;
- string filePath = System.IO.Path.Combine(Application.streamingAssetsPath + "/bd", assetBundleName);
- #if UNITY_EDITOR_OSX
- AssetBundleCreateRequest assetBundle = AssetBundle.LoadFromFileAsync(filePath);
- yield return assetBundle;
- ab = assetBundle.assetBundle;
- // 加载AssetBundle
- #else
- UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath);
- yield return req.SendWebRequest();
- if (req.error == null)
- {
- ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
- }
- else
- {
- loadtext.GetComponent<Text>().text=req.error;
- }
- #endif
- if(ab)
- {
- AssetBundleRequest abr =ab.LoadAssetAsync<GameObject>(assetBundleName);
- yield return abr;
- GameObject Model = GameObject.Instantiate((GameObject)abr.asset);
- Model.transform.SetParent(this.transform);
- Model.transform.localEulerAngles = Vector3.zero;
- Model.transform.localPosition = Vector3.zero;
- Model.transform.localScale = Vector3.one;
- Model.name = assetBundleName+"_Model";
- ModelDic.Add(assetBundleName,Model);
- loadtext.SetActive(false);
- }
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- }
|