using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using AIPagedLod; using UnityEngine.UI; using System.Xml; public class RuntimeDataLoader : MonoBehaviour { public bool mIsAutoLoad = false; public DataPathInfo mDataPathInfo; public bool mIsLoadFinished = false; public Vector3 mCenterPosition = Vector3.zero; public GameObject mDataLoadInfoItem; private GameObject mLoadManagerPrefab; private LoadPagedLodFromFileManager mCurrentLoadManager; private List mNeedPagedLodLoaderList = new List(); private int mCurrentTileIndex = 0; private int mLoadedCount = 0; private bool mIsGetNeedLoadFiles = false; private Vector3 mMinPos = Vector3.zero; private Vector3 mMaxPos = Vector3.zero; void Start() { if(mIsAutoLoad) { StartLoad(); } } public void StartLoad() { if (PagedLodConfig.mInstance.mDataPathDict.ContainsKey(mDataPathInfo.mDataName)) { mDataPathInfo = PagedLodConfig.mInstance.mDataPathDict[mDataPathInfo.mDataName]; } mLoadManagerPrefab = Resources.Load("LoadManagerPrefab") as GameObject; GameObject loadManagerObject = GameObject.Instantiate(mLoadManagerPrefab); LoadPagedLodFromFileManager loadManager = loadManagerObject.GetComponent(); loadManagerObject.transform.SetParent(transform); loadManagerObject.transform.localPosition = Vector3.zero; loadManagerObject.transform.localEulerAngles = Vector3.zero; loadManagerObject.transform.localScale = Vector3.one; loadManager.mConfig.mDataName = mDataPathInfo.mDataName; loadManager.mConfig.mDataPath = mDataPathInfo.mDataPath; loadManager.mConfig.mStartLevel = LoadPagedLodFromFileManager.GetDataStartLevel(mDataPathInfo.mDataPath); loadManager.mConfig.mIsUpdate = false; loadManager.mConfig.mIsDaJiangData = mDataPathInfo.mIsDaJiangData; mCurrentLoadManager = loadManager; GetNeedLoadFiles(); GetDataCenterFromConfigFile(); } void GetDataCenterFromConfigFile() { string dataPath = mDataPathInfo.mDataPath; string configFile = Directory.GetParent(dataPath).FullName + "/DataCenter.xml"; if (File.Exists(configFile)) { string[] strCenters = File.ReadAllText(configFile, System.Text.Encoding.ASCII).Split(','); mCenterPosition.x = float.Parse(strCenters[0]); mCenterPosition.y = float.Parse(strCenters[1]); mCenterPosition.z = float.Parse(strCenters[2]); if (LoadDataRumtime.mSetCameraPositionFlag) { LoadDataRumtime.SetCameraToCenter(mCenterPosition); LoadDataRumtime.mSetCameraPositionFlag = false; } } } public void SaveDataCenterToConfigFile() { string dataPath = mDataPathInfo.mDataPath; string configFile = Directory.GetParent(dataPath).FullName + "/DataCenter.xml"; StreamWriter sw = new StreamWriter(configFile); string strCenter = mCenterPosition.x.ToString() +"," + mCenterPosition.y.ToString() + "," + mCenterPosition.z.ToString(); sw.Write(strCenter); sw.Flush(); sw.Close(); } void GetNeedLoadFilesFromConfigFile(string configFile ,int loadMinLevel) { string dataPath = mDataPathInfo.mDataPath; string[] subFiles = File.ReadAllLines(configFile, System.Text.Encoding.ASCII); for (int i = 0; i < subFiles.Length; ++i) { string fileName = dataPath + "/" + subFiles[i]; if (File.Exists(fileName)) { FileInfo fileInfo = new FileInfo(fileName); PagedLodFileLoader pagedLodLoader = PagedLodFileLoader.GetPagedLodFileLoader(fileInfo, loadMinLevel, fileInfo.Directory.Name, mCurrentLoadManager.mConfig.mIsDaJiangData); mNeedPagedLodLoaderList.Add(pagedLodLoader); } } } void GetNeedLoadFileFromDir(string dataPath, int loadMinLevel) { DirectoryInfo dataDir = new DirectoryInfo(dataPath); List dirInfos = new List(dataDir.GetDirectories()); for (int i = 0; i < dirInfos.Count; ++i) { DirectoryInfo tileDir = dirInfos[i]; if (loadMinLevel <= 0) { string fileName = tileDir.FullName + "/" + tileDir.Name + PagedLodConfig.mInstance.GetFileSufix(); if (File.Exists(fileName)) { FileInfo fileInfo = new FileInfo(fileName); PagedLodFileLoader pagedLodLoader = PagedLodFileLoader.GetPagedLodFileLoader(fileInfo, mCurrentLoadManager.mConfig.mStartLevel, tileDir.Name, mCurrentLoadManager.mConfig.mIsDaJiangData); mNeedPagedLodLoaderList.Add(pagedLodLoader); } } else { bool isExist = false; foreach (var fileInfo in tileDir.GetFiles("*"+ PagedLodConfig.mInstance.GetFileSufix())) { int level = LoadPagedLodFromFileManager.GetTileFileLevel(tileDir.Name, fileInfo.Name); if (level == loadMinLevel) { PagedLodFileLoader pagedLodLoader = PagedLodFileLoader.GetPagedLodFileLoader(fileInfo, level, tileDir.Name, mCurrentLoadManager.mConfig.mIsDaJiangData); mNeedPagedLodLoaderList.Add(pagedLodLoader); isExist = true; } if (level > loadMinLevel) { if (!isExist) { Debug.Log(tileDir.Name + " StartLevel " + loadMinLevel + " Not Exist File "); } break; } } } } } void GetNeedLoadFiles() { PagedLodThreadPool.RunAsync(() => { string dataPath = mDataPathInfo.mDataPath; int loadMinLevel = mDataPathInfo.mMinLevel; string configFile = Directory.GetParent(dataPath).FullName + string.Format("/DataFileIndex_{0}.xml", loadMinLevel); if (loadMinLevel <= 0) //加载数据起始层级 { GetNeedLoadFileFromDir(dataPath, loadMinLevel); } else //加载指定最小层级 { if (File.Exists(configFile)) { GetNeedLoadFilesFromConfigFile(configFile, loadMinLevel); } else { GetNeedLoadFileFromDir(dataPath, loadMinLevel); } } PagedLodThreadPool.QueueOnMainThread(() => { mIsGetNeedLoadFiles = true; }); }); } void Update() { if (!mIsGetNeedLoadFiles) return; if (mIsLoadFinished) return; List pagedLodLoaderList = new List(); for (int i = 0; i < 10; ++i) { if (mCurrentTileIndex < mNeedPagedLodLoaderList.Count) { pagedLodLoaderList.Add(mNeedPagedLodLoaderList[mCurrentTileIndex]); mCurrentTileIndex++; } } PagedLodThreadPool.RunAsync(() => { for (int i = 0; i < pagedLodLoaderList.Count; ++i) { pagedLodLoaderList[i].LoadObjectBasicInfo(); pagedLodLoaderList[i].LoadObjectMeshInfo(); mNeedPagedLodLoaderList[i].StartLoad(); } PagedLodThreadPool.QueueOnMainThread(() => { for (int i = 0; i < pagedLodLoaderList.Count; ++i) { LoadPagedLod(pagedLodLoaderList[i]); mLoadedCount++; SetItemText("正在加载:" + InfoToText()); if (mLoadedCount == mNeedPagedLodLoaderList.Count) { mIsLoadFinished = true; StartUpdateLod(); if (mCenterPosition == Vector3.zero) { mCenterPosition = (mMinPos + mMaxPos) / 2; mCenterPosition.y = mMinPos.y; mCenterPosition = GetHitPosition(mCenterPosition); SaveDataCenterToConfigFile(); } } } for (int i = 0; i < pagedLodLoaderList.Count; ++i) { pagedLodLoaderList[i].Dispose(); pagedLodLoaderList[i] = null; } }); }); } void SetItemText(string text) { if (mDataLoadInfoItem != null) { mDataLoadInfoItem.GetComponentInChildren().text = text; } } void LoadPagedLod(PagedLodFileLoader loader) { PagedLod pagedLod = LoadPagedLodFromFileManager.LoadPagedLod(loader, mCurrentLoadManager.transform); if (PagedLodConfig.mInstance.mTileDataType == TileDataType.OSGB) { pagedLod.LoadOsgbBasicInfo(loader); } else { pagedLod.LoadTileSetJsonFile(); pagedLod.LoadB3dmBasicInfo(loader); } pagedLod.GetMeshRenders(); pagedLod.mIsRootTile = true; LoadPagedLodFromFileManager.GenerateColliderObject(pagedLod); if (mCenterPosition == Vector3.zero) { if (mMinPos == Vector3.zero) { mMinPos = pagedLod.mWorldCenter; mMaxPos = pagedLod.mWorldCenter; } else { mMinPos.x = Mathf.Min(mMinPos.x, pagedLod.mWorldCenter.x); mMinPos.y = Mathf.Min(mMinPos.y, pagedLod.mWorldCenter.y); mMinPos.z = Mathf.Min(mMinPos.z, pagedLod.mWorldCenter.z); mMaxPos.x = Mathf.Max(mMaxPos.x, pagedLod.mWorldCenter.x); mMaxPos.y = Mathf.Max(mMaxPos.y, pagedLod.mWorldCenter.y); mMaxPos.z = Mathf.Max(mMaxPos.z, pagedLod.mWorldCenter.z); } if (LoadDataRumtime.mSetCameraPositionFlag) { LoadDataRumtime.SetCameraToCenter(pagedLod.mWorldCenter); LoadDataRumtime.mSetCameraPositionFlag = false; } } } string InfoToText() { return mDataPathInfo.mDataName + "(" + mNeedPagedLodLoaderList.Count.ToString() + "/" + mLoadedCount.ToString() + ")"; } public void StartUpdateLod() { if (mDataPathInfo.mLodEnable) { mCurrentLoadManager.mConfig.mIsUpdate = true; } } Vector3 GetHitPosition(Vector3 pos) { RaycastHit hitInfo; Vector3 startPos = pos + new Vector3(0, 1000, 0); Vector3 endPos = pos + new Vector3(0, -1000, 0); if (Physics.Linecast(startPos,endPos,out hitInfo)) { return hitInfo.point; } return pos; } }