| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 | 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<PagedLodFileLoader> mNeedPagedLodLoaderList = new List<PagedLodFileLoader>();    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<LoadPagedLodFromFileManager>();        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<DirectoryInfo> dirInfos = new List<DirectoryInfo>(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<PagedLodFileLoader> pagedLodLoaderList = new List<PagedLodFileLoader>();        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 = 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;    }}
 |