123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- 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<string, B3dmBasicInfo> mBasicInfoDict = new Dictionary<string, B3dmBasicInfo>();//���ڵ�ӵ��
- 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<TileUrlInfo> mTileUrlList = new List<TileUrlInfo>();
- 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<LoadPagedLodFromFileManager>();
- 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<AroundCamera>();
- MouseTranslate translate = around.target.GetComponent<MouseTranslate>();
- 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<AroundCamera>() != null)
- {
- AroundCamera around = Camera.main.GetComponent<AroundCamera>();
- MouseTranslate translate = around.target.GetComponent<MouseTranslate>();
- 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<MeshRenderer>();
- 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);
- }
- }
- }
|