using AIPagedLod; using LitJson; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using UnityEngine.Events; using Unity.VisualScripting; using DG.Tweening; using UnityAsync; using System.Threading.Tasks; using UnityEngine.UIElements; [System.Serializable] public class TileUrlInfo { public string mTileSetUrl; public string mTileFileUrl; public Dictionary mBasicInfoDict = new Dictionary();//���ڵ�ӵ�� public B3dmBasicInfo mBasicInfo = new B3dmBasicInfo(); public PagedLod mPagedLod; } public class LoadDataFromUrl : MonoBehaviour { public bool mIsAutoLoad = false; public string mTileSetUrl = ""; public string mTileSetUrlLocal = ""; public bool useLocalUrl = true; public List mTileUrlList = new List(); public GameObject mLoadManagerPrefab; public Vector3 mDataCenter; [HideInInspector] public int mCurrentLoadTileSetIndex = 0; [HideInInspector] public int mTileSetLoadedCount = 0; [HideInInspector] public int mCurrentLoadTileFileIndex = 0; [HideInInspector] public int mTileFileLoadedCount = 0; public UnityEvent mLoadRootTileSetEvent; public UnityEvent mLoadTileSetEvent; public UnityEvent mLoadTileFileEvent; protected string mBaseUrl; protected bool mIsStartLoadTileSetJson = false; protected bool mIsStartLoadTileFile = false; public bool testCenterPos = false; public Vector3 centerPos = new Vector3(0, 0, 0); [HideInInspector] public LoadPagedLodFromFileManager mCurrentLoadManager; public Vector3 foucusPos; public B3dmBasicInfo currentBasicInfo; void Start() { if(mIsAutoLoad) { PagedLodConfig.mInstance.mIsLoadFromUrl = true; StartDownloadRootTileSetJson(); } } #region --------------------------StartDownloadTileFile public void StartDownloadTileFile() { CreateLoadManager(); mIsStartLoadTileFile = true; } void CreateLoadManager() { 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 = ""; loadManager.mConfig.mDataPath = ""; loadManager.mConfig.mIsUpdate = false; loadManager.mConfig.mBaseUrl = mBaseUrl; mCurrentLoadManager = loadManager; } #endregion #region ---------------------StartLoadTileSetJson private void Update() { if (testCenterPos) { Vector3 center = transform.TransformPoint(centerPos); AroundCamera around = Camera.main.GetComponent(); MouseTranslate translate = around.target.GetComponent(); translate.transform.position = center; translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position; translate.enabled = true; } if (mIsStartLoadTileSetJson) { if (DownloadTileSetManager.mInstance.CanDownload()) { if (mCurrentLoadTileSetIndex < mTileUrlList.Count) { TileUrlInfo info = mTileUrlList[mCurrentLoadTileSetIndex]; DownloadTileSetManager.mInstance.StartDownloadTileSetJson(this, info); } mCurrentLoadTileSetIndex++; } } else if (mIsStartLoadTileFile) { if (DownloadTileFileManager.mInstance.CanDownload()) { if (mCurrentLoadTileFileIndex < mTileUrlList.Count) { TileUrlInfo info = mTileUrlList[mCurrentLoadTileFileIndex]; DownloadTileFileManager.mInstance.StartDownloadTileFile(this, info); } mCurrentLoadTileFileIndex++; } } } public void DoLoadTileFileFinished() { mTileFileLoadedCount++; mLoadTileFileEvent.Invoke(); if (mTileFileLoadedCount == mTileUrlList.Count) //ȫ��TileFile������� { mIsStartLoadTileFile = false; mCurrentLoadTileFileIndex = 0; mCurrentLoadManager.mConfig.mIsUpdate = true; } } public void DoLoadTileSetFinished() { mTileSetLoadedCount++; mLoadTileSetEvent.Invoke(); if(mTileSetLoadedCount == mTileUrlList.Count) //ȫ��TileSet������� { mIsStartLoadTileSetJson = false; mCurrentLoadTileFileIndex = 0; if(mIsAutoLoad) { StartDownloadTileFile();//��ʼ������Ƭ���� } } } #endregion #region -----------------------StartDownloadRootTileSetJson //�������������ݵ�tileset.Json�ļ� public void StartDownloadRootTileSetJson() { StartCoroutine(DownloadRootTileSetJson()); } public IEnumerator DownloadRootTileSetJson() { string url = mTileSetUrl; if (useLocalUrl) { mTileSetUrlLocal = Application.streamingAssetsPath + "/" + mTileSetUrlLocal; url = mTileSetUrlLocal; } mBaseUrl = url.Replace("tileset.json", ""); UnityWebRequest www = UnityWebRequest.Get(url); www.timeout = 10; yield return www.SendWebRequest(); if (www.isHttpError || www.isNetworkError) { Debug.Log(www.error + " " + url); } else { JsonData resultJson = JsonMapper.ToObject(www.downloadHandler.text); JsonData tileJson = resultJson["root"]; B3dmBasicInfo basicInfo = PagedLod.LoadTileBasicInfo(tileJson); currentBasicInfo = basicInfo; //Vector3 center = transform.TransformPoint(basicInfo.mBounds.center); //mDataCenter = center; //SetCameraToCenter(center); JsonData children = tileJson["children"]; for (int i = 0; i < children.Count; ++i) { string childUrl = children[i]["content"]["uri"].ToString(); string tileUrl = mBaseUrl + childUrl.Replace("./",""); TileUrlInfo info = new TileUrlInfo(); info.mTileSetUrl = tileUrl; mTileUrlList.Add(info); } mCurrentLoadTileSetIndex = 0; mIsStartLoadTileSetJson = true; mLoadRootTileSetEvent.Invoke(); } } #endregion public void ForceSacnMode() { if (!this.gameObject.activeSelf) this.gameObject.SetActive(true); SetCameraToCenter(foucusPos); } public async Task SetCameraToCenter(Vector3 centerPos = default(Vector3)) { if (Camera.main.GetComponent() != null) { AroundCamera around = Camera.main.GetComponent(); MouseTranslate translate = around.target.GetComponent(); translate.enabled = false; if (centerPos == Vector3.zero) { await Await.Until(() => { return currentBasicInfo.mBounds.center != Vector3.zero; }); print("�������"); print(currentBasicInfo.mBounds.center); Vector3 center = transform.TransformPoint(currentBasicInfo.mBounds.center); translate.transform.position = center; translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position; translate.enabled = true; } else { MeshRenderer fader = Camera.main.transform.GetChild(0).GetComponent(); fader.gameObject.SetActive(true); fader.material.DOColor(Color.white, 0.1f).onComplete = () => { fader.material.DOColor(Color.clear, 1.5f).onComplete = () => { fader.gameObject.SetActive(false); }; }; Vector3 center = transform.TransformPoint(centerPos); translate.transform.position = center; translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position; translate.enabled = true; } //translate.transform.position = center; //translate.CurrentOffset = translate.targetOffset = center - translate.areaSettings.center.position; //around.target.position = center; } } public void ResetTile() { mTileUrlList.Clear(); mCurrentLoadTileSetIndex = 0; mCurrentLoadTileFileIndex = 0; mTileSetLoadedCount = 0; mTileFileLoadedCount = 0; if (mCurrentLoadManager != null) { foreach(var item in mCurrentLoadManager.mPagedLodList) { item.CollectChildTiles(); } GameObject.Destroy(mCurrentLoadManager.gameObject); } } }