RuntimeDataLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using AIPagedLod;
  6. using UnityEngine.UI;
  7. using System.Xml;
  8. public class RuntimeDataLoader : MonoBehaviour
  9. {
  10. public bool mIsAutoLoad = false;
  11. public DataPathInfo mDataPathInfo;
  12. public bool mIsLoadFinished = false;
  13. public Vector3 mCenterPosition = Vector3.zero;
  14. public GameObject mDataLoadInfoItem;
  15. private GameObject mLoadManagerPrefab;
  16. private LoadPagedLodFromFileManager mCurrentLoadManager;
  17. private List<PagedLodFileLoader> mNeedPagedLodLoaderList = new List<PagedLodFileLoader>();
  18. private int mCurrentTileIndex = 0;
  19. private int mLoadedCount = 0;
  20. private bool mIsGetNeedLoadFiles = false;
  21. private Vector3 mMinPos = Vector3.zero;
  22. private Vector3 mMaxPos = Vector3.zero;
  23. void Start()
  24. {
  25. if(mIsAutoLoad)
  26. {
  27. StartLoad();
  28. }
  29. }
  30. public void StartLoad()
  31. {
  32. if (PagedLodConfig.mInstance.mDataPathDict.ContainsKey(mDataPathInfo.mDataName))
  33. {
  34. mDataPathInfo = PagedLodConfig.mInstance.mDataPathDict[mDataPathInfo.mDataName];
  35. }
  36. mLoadManagerPrefab = Resources.Load("LoadManagerPrefab") as GameObject;
  37. GameObject loadManagerObject = GameObject.Instantiate(mLoadManagerPrefab);
  38. LoadPagedLodFromFileManager loadManager = loadManagerObject.GetComponent<LoadPagedLodFromFileManager>();
  39. loadManagerObject.transform.SetParent(transform);
  40. loadManagerObject.transform.localPosition = Vector3.zero;
  41. loadManagerObject.transform.localEulerAngles = Vector3.zero;
  42. loadManagerObject.transform.localScale = Vector3.one;
  43. loadManager.mConfig.mDataName = mDataPathInfo.mDataName;
  44. loadManager.mConfig.mDataPath = mDataPathInfo.mDataPath;
  45. loadManager.mConfig.mStartLevel = LoadPagedLodFromFileManager.GetDataStartLevel(mDataPathInfo.mDataPath);
  46. loadManager.mConfig.mIsUpdate = false;
  47. loadManager.mConfig.mIsDaJiangData = mDataPathInfo.mIsDaJiangData;
  48. mCurrentLoadManager = loadManager;
  49. GetNeedLoadFiles();
  50. GetDataCenterFromConfigFile();
  51. }
  52. void GetDataCenterFromConfigFile()
  53. {
  54. string dataPath = mDataPathInfo.mDataPath;
  55. string configFile = Directory.GetParent(dataPath).FullName + "/DataCenter.xml";
  56. if (File.Exists(configFile))
  57. {
  58. string[] strCenters = File.ReadAllText(configFile, System.Text.Encoding.ASCII).Split(',');
  59. mCenterPosition.x = float.Parse(strCenters[0]);
  60. mCenterPosition.y = float.Parse(strCenters[1]);
  61. mCenterPosition.z = float.Parse(strCenters[2]);
  62. if (LoadDataRumtime.mSetCameraPositionFlag)
  63. {
  64. LoadDataRumtime.SetCameraToCenter(mCenterPosition);
  65. LoadDataRumtime.mSetCameraPositionFlag = false;
  66. }
  67. }
  68. }
  69. public void SaveDataCenterToConfigFile()
  70. {
  71. string dataPath = mDataPathInfo.mDataPath;
  72. string configFile = Directory.GetParent(dataPath).FullName + "/DataCenter.xml";
  73. StreamWriter sw = new StreamWriter(configFile);
  74. string strCenter = mCenterPosition.x.ToString() +"," + mCenterPosition.y.ToString() + "," + mCenterPosition.z.ToString();
  75. sw.Write(strCenter);
  76. sw.Flush();
  77. sw.Close();
  78. }
  79. void GetNeedLoadFilesFromConfigFile(string configFile ,int loadMinLevel)
  80. {
  81. string dataPath = mDataPathInfo.mDataPath;
  82. string[] subFiles = File.ReadAllLines(configFile, System.Text.Encoding.ASCII);
  83. for (int i = 0; i < subFiles.Length; ++i)
  84. {
  85. string fileName = dataPath + "/" + subFiles[i];
  86. if (File.Exists(fileName))
  87. {
  88. FileInfo fileInfo = new FileInfo(fileName);
  89. PagedLodFileLoader pagedLodLoader = PagedLodFileLoader.GetPagedLodFileLoader(fileInfo, loadMinLevel,
  90. fileInfo.Directory.Name, mCurrentLoadManager.mConfig.mIsDaJiangData);
  91. mNeedPagedLodLoaderList.Add(pagedLodLoader);
  92. }
  93. }
  94. }
  95. void GetNeedLoadFileFromDir(string dataPath, int loadMinLevel)
  96. {
  97. DirectoryInfo dataDir = new DirectoryInfo(dataPath);
  98. List<DirectoryInfo> dirInfos = new List<DirectoryInfo>(dataDir.GetDirectories());
  99. for (int i = 0; i < dirInfos.Count; ++i)
  100. {
  101. DirectoryInfo tileDir = dirInfos[i];
  102. if (loadMinLevel <= 0)
  103. {
  104. string fileName = tileDir.FullName + "/" + tileDir.Name + PagedLodConfig.mInstance.GetFileSufix();
  105. if (File.Exists(fileName))
  106. {
  107. FileInfo fileInfo = new FileInfo(fileName);
  108. PagedLodFileLoader pagedLodLoader = PagedLodFileLoader.GetPagedLodFileLoader(fileInfo, mCurrentLoadManager.mConfig.mStartLevel,
  109. tileDir.Name, mCurrentLoadManager.mConfig.mIsDaJiangData);
  110. mNeedPagedLodLoaderList.Add(pagedLodLoader);
  111. }
  112. }
  113. else
  114. {
  115. bool isExist = false;
  116. foreach (var fileInfo in tileDir.GetFiles("*"+ PagedLodConfig.mInstance.GetFileSufix()))
  117. {
  118. int level = LoadPagedLodFromFileManager.GetTileFileLevel(tileDir.Name, fileInfo.Name);
  119. if (level == loadMinLevel)
  120. {
  121. PagedLodFileLoader pagedLodLoader = PagedLodFileLoader.GetPagedLodFileLoader(fileInfo, level,
  122. tileDir.Name, mCurrentLoadManager.mConfig.mIsDaJiangData);
  123. mNeedPagedLodLoaderList.Add(pagedLodLoader);
  124. isExist = true;
  125. }
  126. if (level > loadMinLevel)
  127. {
  128. if (!isExist)
  129. {
  130. Debug.Log(tileDir.Name + " StartLevel " + loadMinLevel + " Not Exist File ");
  131. }
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. void GetNeedLoadFiles()
  139. {
  140. PagedLodThreadPool.RunAsync(() =>
  141. {
  142. string dataPath = mDataPathInfo.mDataPath;
  143. int loadMinLevel = mDataPathInfo.mMinLevel;
  144. string configFile = Directory.GetParent(dataPath).FullName + string.Format("/DataFileIndex_{0}.xml", loadMinLevel);
  145. if (loadMinLevel <= 0) //加载数据起始层级
  146. {
  147. GetNeedLoadFileFromDir(dataPath, loadMinLevel);
  148. }
  149. else //加载指定最小层级
  150. {
  151. if (File.Exists(configFile))
  152. {
  153. GetNeedLoadFilesFromConfigFile(configFile, loadMinLevel);
  154. }
  155. else
  156. {
  157. GetNeedLoadFileFromDir(dataPath, loadMinLevel);
  158. }
  159. }
  160. PagedLodThreadPool.QueueOnMainThread(() =>
  161. {
  162. mIsGetNeedLoadFiles = true;
  163. });
  164. });
  165. }
  166. void Update()
  167. {
  168. if (!mIsGetNeedLoadFiles) return;
  169. if (mIsLoadFinished) return;
  170. List<PagedLodFileLoader> pagedLodLoaderList = new List<PagedLodFileLoader>();
  171. for (int i = 0; i < 10; ++i)
  172. {
  173. if (mCurrentTileIndex < mNeedPagedLodLoaderList.Count)
  174. {
  175. pagedLodLoaderList.Add(mNeedPagedLodLoaderList[mCurrentTileIndex]);
  176. mCurrentTileIndex++;
  177. }
  178. }
  179. PagedLodThreadPool.RunAsync(() =>
  180. {
  181. for (int i = 0; i < pagedLodLoaderList.Count; ++i)
  182. {
  183. pagedLodLoaderList[i].LoadObjectBasicInfo();
  184. pagedLodLoaderList[i].LoadObjectMeshInfo();
  185. mNeedPagedLodLoaderList[i].StartLoad();
  186. }
  187. PagedLodThreadPool.QueueOnMainThread(() =>
  188. {
  189. for (int i = 0; i < pagedLodLoaderList.Count; ++i)
  190. {
  191. LoadPagedLod(pagedLodLoaderList[i]);
  192. mLoadedCount++;
  193. SetItemText("正在加载:" + InfoToText());
  194. if (mLoadedCount == mNeedPagedLodLoaderList.Count)
  195. {
  196. mIsLoadFinished = true;
  197. StartUpdateLod();
  198. if (mCenterPosition == Vector3.zero)
  199. {
  200. mCenterPosition = (mMinPos + mMaxPos) / 2;
  201. mCenterPosition.y = mMinPos.y;
  202. mCenterPosition = GetHitPosition(mCenterPosition);
  203. SaveDataCenterToConfigFile();
  204. }
  205. }
  206. }
  207. for (int i = 0; i < pagedLodLoaderList.Count; ++i)
  208. {
  209. pagedLodLoaderList[i].Dispose();
  210. pagedLodLoaderList[i] = null;
  211. }
  212. });
  213. });
  214. }
  215. void SetItemText(string text)
  216. {
  217. if (mDataLoadInfoItem != null)
  218. {
  219. mDataLoadInfoItem.GetComponentInChildren<Text>().text = text;
  220. }
  221. }
  222. void LoadPagedLod(PagedLodFileLoader loader)
  223. {
  224. PagedLod pagedLod = LoadPagedLodFromFileManager.LoadPagedLod(loader, mCurrentLoadManager.transform);
  225. if (PagedLodConfig.mInstance.mTileDataType == TileDataType.OSGB)
  226. {
  227. pagedLod.LoadOsgbBasicInfo(loader);
  228. }
  229. else
  230. {
  231. pagedLod.LoadTileSetJsonFile();
  232. pagedLod.LoadB3dmBasicInfo(loader);
  233. }
  234. pagedLod.GetMeshRenders();
  235. pagedLod.mIsRootTile = true;
  236. LoadPagedLodFromFileManager.GenerateColliderObject(pagedLod);
  237. if (mCenterPosition == Vector3.zero)
  238. {
  239. if (mMinPos == Vector3.zero)
  240. {
  241. mMinPos = pagedLod.mWorldCenter;
  242. mMaxPos = pagedLod.mWorldCenter;
  243. }
  244. else
  245. {
  246. mMinPos.x = Mathf.Min(mMinPos.x, pagedLod.mWorldCenter.x);
  247. mMinPos.y = Mathf.Min(mMinPos.y, pagedLod.mWorldCenter.y);
  248. mMinPos.z = Mathf.Min(mMinPos.z, pagedLod.mWorldCenter.z);
  249. mMaxPos.x = Mathf.Max(mMaxPos.x, pagedLod.mWorldCenter.x);
  250. mMaxPos.y = Mathf.Max(mMaxPos.y, pagedLod.mWorldCenter.y);
  251. mMaxPos.z = Mathf.Max(mMaxPos.z, pagedLod.mWorldCenter.z);
  252. }
  253. if (LoadDataRumtime.mSetCameraPositionFlag)
  254. {
  255. LoadDataRumtime.SetCameraToCenter(pagedLod.mWorldCenter);
  256. LoadDataRumtime.mSetCameraPositionFlag = false;
  257. }
  258. }
  259. }
  260. string InfoToText()
  261. {
  262. return mDataPathInfo.mDataName + "(" + mNeedPagedLodLoaderList.Count.ToString() + "/" + mLoadedCount.ToString() + ")";
  263. }
  264. public void StartUpdateLod()
  265. {
  266. if (mDataPathInfo.mLodEnable)
  267. {
  268. mCurrentLoadManager.mConfig.mIsUpdate = true;
  269. }
  270. }
  271. Vector3 GetHitPosition(Vector3 pos)
  272. {
  273. RaycastHit hitInfo;
  274. Vector3 startPos = pos + new Vector3(0, 1000, 0);
  275. Vector3 endPos = pos + new Vector3(0, -1000, 0);
  276. if (Physics.Linecast(startPos,endPos,out hitInfo))
  277. {
  278. return hitInfo.point;
  279. }
  280. return pos;
  281. }
  282. }