LoadDataFromUrl.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using AIPagedLod;
  2. using LitJson;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. using UnityEngine.UI;
  8. using UnityEngine.Events;
  9. using Unity.VisualScripting;
  10. using DG.Tweening;
  11. using UnityAsync;
  12. using System.Threading.Tasks;
  13. using UnityEngine.UIElements;
  14. [System.Serializable]
  15. public class TileUrlInfo
  16. {
  17. public string mTileSetUrl;
  18. public string mTileFileUrl;
  19. public Dictionary<string, B3dmBasicInfo> mBasicInfoDict = new Dictionary<string, B3dmBasicInfo>();//���ڵ�ӵ��
  20. public B3dmBasicInfo mBasicInfo = new B3dmBasicInfo();
  21. public PagedLod mPagedLod;
  22. }
  23. public class LoadDataFromUrl : MonoBehaviour
  24. {
  25. public bool mIsAutoLoad = false;
  26. public string mTileSetUrl = "";
  27. public string mTileSetUrlLocal = "";
  28. public bool useLocalUrl = true;
  29. public List<TileUrlInfo> mTileUrlList = new List<TileUrlInfo>();
  30. public GameObject mLoadManagerPrefab;
  31. public Vector3 mDataCenter;
  32. [HideInInspector] public int mCurrentLoadTileSetIndex = 0;
  33. [HideInInspector] public int mTileSetLoadedCount = 0;
  34. [HideInInspector] public int mCurrentLoadTileFileIndex = 0;
  35. [HideInInspector] public int mTileFileLoadedCount = 0;
  36. public UnityEvent mLoadRootTileSetEvent;
  37. public UnityEvent mLoadTileSetEvent;
  38. public UnityEvent mLoadTileFileEvent;
  39. protected string mBaseUrl;
  40. protected bool mIsStartLoadTileSetJson = false;
  41. protected bool mIsStartLoadTileFile = false;
  42. public bool testCenterPos = false;
  43. public Vector3 centerPos = new Vector3(0, 0, 0);
  44. [HideInInspector] public LoadPagedLodFromFileManager mCurrentLoadManager;
  45. public Vector3 foucusPos;
  46. public B3dmBasicInfo currentBasicInfo;
  47. void Start()
  48. {
  49. if(mIsAutoLoad)
  50. {
  51. PagedLodConfig.mInstance.mIsLoadFromUrl = true;
  52. StartDownloadRootTileSetJson();
  53. }
  54. }
  55. #region --------------------------StartDownloadTileFile
  56. public void StartDownloadTileFile()
  57. {
  58. CreateLoadManager();
  59. mIsStartLoadTileFile = true;
  60. }
  61. void CreateLoadManager()
  62. {
  63. GameObject loadManagerObject = GameObject.Instantiate(mLoadManagerPrefab);
  64. LoadPagedLodFromFileManager loadManager = loadManagerObject.GetComponent<LoadPagedLodFromFileManager>();
  65. loadManagerObject.transform.SetParent(transform);
  66. loadManagerObject.transform.localPosition = Vector3.zero;
  67. loadManagerObject.transform.localEulerAngles = Vector3.zero;
  68. loadManagerObject.transform.localScale = Vector3.one;
  69. loadManager.mConfig.mDataName = "";
  70. loadManager.mConfig.mDataPath = "";
  71. loadManager.mConfig.mIsUpdate = false;
  72. loadManager.mConfig.mBaseUrl = mBaseUrl;
  73. mCurrentLoadManager = loadManager;
  74. }
  75. #endregion
  76. #region ---------------------StartLoadTileSetJson
  77. private void Update()
  78. {
  79. if (testCenterPos) {
  80. Vector3 center = transform.TransformPoint(centerPos);
  81. AroundCamera around = Camera.main.GetComponent<AroundCamera>();
  82. MouseTranslate translate = around.target.GetComponent<MouseTranslate>();
  83. translate.transform.position = center;
  84. translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position;
  85. translate.enabled = true;
  86. }
  87. if (mIsStartLoadTileSetJson)
  88. {
  89. if (DownloadTileSetManager.mInstance.CanDownload())
  90. {
  91. if (mCurrentLoadTileSetIndex < mTileUrlList.Count)
  92. {
  93. TileUrlInfo info = mTileUrlList[mCurrentLoadTileSetIndex];
  94. DownloadTileSetManager.mInstance.StartDownloadTileSetJson(this, info);
  95. }
  96. mCurrentLoadTileSetIndex++;
  97. }
  98. }
  99. else if (mIsStartLoadTileFile)
  100. {
  101. if (DownloadTileFileManager.mInstance.CanDownload())
  102. {
  103. if (mCurrentLoadTileFileIndex < mTileUrlList.Count)
  104. {
  105. TileUrlInfo info = mTileUrlList[mCurrentLoadTileFileIndex];
  106. DownloadTileFileManager.mInstance.StartDownloadTileFile(this, info);
  107. }
  108. mCurrentLoadTileFileIndex++;
  109. }
  110. }
  111. }
  112. public void DoLoadTileFileFinished()
  113. {
  114. mTileFileLoadedCount++;
  115. mLoadTileFileEvent.Invoke();
  116. if (mTileFileLoadedCount == mTileUrlList.Count) //ȫ��TileFile�������
  117. {
  118. mIsStartLoadTileFile = false;
  119. mCurrentLoadTileFileIndex = 0;
  120. mCurrentLoadManager.mConfig.mIsUpdate = true;
  121. }
  122. }
  123. public void DoLoadTileSetFinished()
  124. {
  125. mTileSetLoadedCount++;
  126. mLoadTileSetEvent.Invoke();
  127. if(mTileSetLoadedCount == mTileUrlList.Count) //ȫ��TileSet�������
  128. {
  129. mIsStartLoadTileSetJson = false;
  130. mCurrentLoadTileFileIndex = 0;
  131. if(mIsAutoLoad)
  132. {
  133. StartDownloadTileFile();//��ʼ������Ƭ����
  134. }
  135. }
  136. }
  137. #endregion
  138. #region -----------------------StartDownloadRootTileSetJson
  139. //�������������ݵ�tileset.Json�ļ�
  140. public void StartDownloadRootTileSetJson()
  141. {
  142. StartCoroutine(DownloadRootTileSetJson());
  143. }
  144. public IEnumerator DownloadRootTileSetJson()
  145. {
  146. string url = mTileSetUrl;
  147. if (useLocalUrl) {
  148. mTileSetUrlLocal = Application.streamingAssetsPath + "/" + mTileSetUrlLocal;
  149. url = mTileSetUrlLocal;
  150. }
  151. mBaseUrl = url.Replace("tileset.json", "");
  152. UnityWebRequest www = UnityWebRequest.Get(url);
  153. www.timeout = 10;
  154. yield return www.SendWebRequest();
  155. if (www.isHttpError || www.isNetworkError)
  156. {
  157. Debug.Log(www.error + " " + url);
  158. }
  159. else
  160. {
  161. JsonData resultJson = JsonMapper.ToObject(www.downloadHandler.text);
  162. JsonData tileJson = resultJson["root"];
  163. B3dmBasicInfo basicInfo = PagedLod.LoadTileBasicInfo(tileJson);
  164. currentBasicInfo = basicInfo;
  165. //Vector3 center = transform.TransformPoint(basicInfo.mBounds.center);
  166. //mDataCenter = center;
  167. //SetCameraToCenter(center);
  168. JsonData children = tileJson["children"];
  169. for (int i = 0; i < children.Count; ++i)
  170. {
  171. string childUrl = children[i]["content"]["uri"].ToString();
  172. string tileUrl = mBaseUrl + childUrl.Replace("./","");
  173. TileUrlInfo info = new TileUrlInfo();
  174. info.mTileSetUrl = tileUrl;
  175. mTileUrlList.Add(info);
  176. }
  177. mCurrentLoadTileSetIndex = 0;
  178. mIsStartLoadTileSetJson = true;
  179. mLoadRootTileSetEvent.Invoke();
  180. }
  181. }
  182. #endregion
  183. public void ForceSacnMode() {
  184. if (!this.gameObject.activeSelf) this.gameObject.SetActive(true);
  185. SetCameraToCenter(foucusPos);
  186. }
  187. public async Task SetCameraToCenter(Vector3 centerPos = default(Vector3))
  188. {
  189. if (Camera.main.GetComponent<AroundCamera>() != null)
  190. {
  191. AroundCamera around = Camera.main.GetComponent<AroundCamera>();
  192. MouseTranslate translate = around.target.GetComponent<MouseTranslate>();
  193. translate.enabled = false;
  194. if (centerPos == Vector3.zero)
  195. {
  196. await Await.Until(() =>
  197. {
  198. return currentBasicInfo.mBounds.center != Vector3.zero;
  199. });
  200. print("�������");
  201. print(currentBasicInfo.mBounds.center);
  202. Vector3 center = transform.TransformPoint(currentBasicInfo.mBounds.center);
  203. translate.transform.position = center;
  204. translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position;
  205. translate.enabled = true;
  206. }
  207. else
  208. {
  209. MeshRenderer fader = Camera.main.transform.GetChild(0).GetComponent<MeshRenderer>();
  210. fader.gameObject.SetActive(true);
  211. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  212. {
  213. fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
  214. {
  215. fader.gameObject.SetActive(false);
  216. };
  217. };
  218. Vector3 center = transform.TransformPoint(centerPos);
  219. translate.transform.position = center;
  220. translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position;
  221. translate.enabled = true;
  222. }
  223. //translate.transform.position = center;
  224. //translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position;
  225. //around.target.position = center;
  226. }
  227. }
  228. public void ResetTile()
  229. {
  230. mTileUrlList.Clear();
  231. mCurrentLoadTileSetIndex = 0;
  232. mCurrentLoadTileFileIndex = 0;
  233. mTileSetLoadedCount = 0;
  234. mTileFileLoadedCount = 0;
  235. if (mCurrentLoadManager != null)
  236. {
  237. foreach(var item in mCurrentLoadManager.mPagedLodList)
  238. {
  239. item.CollectChildTiles();
  240. }
  241. GameObject.Destroy(mCurrentLoadManager.gameObject);
  242. }
  243. }
  244. }