DownloadTileFileManager.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using AIPagedLod;
  6. using UnityEngine.Networking;
  7. using LitJson;
  8. public class DownloadTileFileManager : MonoBehaviour
  9. {
  10. public static DownloadTileFileManager mInstance;
  11. public List<UnityWebRequest> mDownloadWWWList = new List<UnityWebRequest>();
  12. public int mMaxCoroutineCount = 2;
  13. public int mCurrentCoroutineCount = 0;
  14. void Awake()
  15. {
  16. mInstance = this;
  17. }
  18. void Update()
  19. {
  20. mDownloadWWWList.RemoveAll(item => item.isDone);
  21. mCurrentCoroutineCount = mDownloadWWWList.Count;
  22. }
  23. public bool CanDownload()
  24. {
  25. return mDownloadWWWList.Count < mMaxCoroutineCount;
  26. }
  27. public void StartDownloadTileFile(LoadDataFromUrl loader,TileUrlInfo info)
  28. {
  29. StartCoroutine(DownloadTileFile(loader,info));
  30. }
  31. public IEnumerator DownloadTileFile(LoadDataFromUrl loadDataFromUrl, TileUrlInfo info)
  32. {
  33. string url = info.mTileFileUrl;
  34. UnityWebRequest www = UnityWebRequest.Get(url);
  35. www.timeout = 10;
  36. yield return www.SendWebRequest();
  37. if (www.isHttpError || www.isNetworkError)
  38. {
  39. Debug.Log(www.error + " " + url);
  40. }
  41. else
  42. {
  43. B3dmFileLoader loader = new B3dmFileLoader();
  44. loader.LoadObjectMeshInfoFromData(www.downloadHandler.data);
  45. loader.mTileName = info.mBasicInfo.mContent;
  46. loader.mTileDirPath = info.mBasicInfo.mContent;
  47. PagedLod pagedLod = LoadPagedLodFromFileManager.LoadPagedLod(loader, loadDataFromUrl.mCurrentLoadManager.transform);
  48. pagedLod.mBasicInfo = info.mBasicInfo;
  49. pagedLod.mBasicInfoDict = info.mBasicInfoDict;
  50. pagedLod.LoadB3dmBasicInfo(loader);
  51. pagedLod.GetMeshRenders();
  52. pagedLod.mIsRootTile = true;
  53. pagedLod.mSelfLevel = 14;
  54. LoadPagedLodFromFileManager.GenerateColliderObject(pagedLod);
  55. loadDataFromUrl.DoLoadTileFileFinished();
  56. }
  57. }
  58. }