using DG.Tweening; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using Adam; using System; using System.IO; using Newtonsoft.Json; public class Bootstrap : MonoSingleton { public LandMarksAndInfoController landMarkAndInfoCotroller; /// /// 0 省会 1 城市 2 区县 /// public int currentLevel = 0; public string currentDisName; public string currentDistCode; private List landMarks = new List(); /// /// 透明 /// public Material[] opacity; /// /// 选中 /// public Material[] select; /// /// 默认 /// public Material[] mat; /// /// 当前选中板块 /// //[HideInInspector] public GameObject currentLand; public CameraRT cameraRt; /// /// 省会 /// public Transform provincialCapital; /// /// 城市 /// public Transform cityParents; /// /// 县级区域 /// public Transform areaParents; private Transform lastCity; private GameObject currentArea; /// /// 城市 /// //public GameObject[] citys; private Dictionary _fadeTweens = new Dictionary(); public Dictionary urlDic = new Dictionary(); [HideInInspector] public WebAdapter webAdapter; [HideInInspector] public RayHitTester rayHitTester; public GameObject lastLand; public GameObject shadowMap; public int clickCurrentLevel = 2; [ContextMenu("1")] private void Awake() { var serverConfig = new System.Uri(Path.Combine(Application.streamingAssetsPath + @"/Config", "WebAddress.json")); GetText(serverConfig.ToString(), (temp) => { if (!String.IsNullOrWhiteSpace(temp)) { Debug.Log("读取到的内容有:" + temp); urlDic = JsonConvert.DeserializeObject>(temp); //urlDic = JsonUtility.FromJson>(temp); } else { Debug.Log("加载配置文件错误"); } }); //string data = FileUtil.ReadFromLocal("WebAddress.json"); //urlDic = JsonConvert.DeserializeObject>(data); webAdapter = GetComponent(); rayHitTester = GetComponent(); } public void GetText(string url, Action actionResult) { StartCoroutine(FileUtil.GetTextAsyn(url, actionResult)); } void Start() { //landMarks = new List { "网络负荷:49.84 kw", "上网负荷:49.84 kw", "削峰负荷:49.84 kw", "填谷负荷:49.84 kw", "发电负荷:49.84 kw" }; landMarkAndInfoCotroller.gameObject.SetActive(false); cameraRt.OnLimitScroll += SwitchLand; } private void Update() { var hit = rayHitTester.HitTest(); if (Input.GetMouseButtonDown(2)) { if (hit.collider != null) { if (cameraRt.currentLevelIndex == 1) return; clickCurrentLevel--; if (clickCurrentLevel <= 1) { clickCurrentLevel = 1; } cameraRt.currentLevelIndex = clickCurrentLevel; //currentLevel = clickCurrentLevel; SwitchLand(clickCurrentLevel); cameraRt.SetTarget(hit.collider.transform); Debug.Log("clickCurrentLevel==" + clickCurrentLevel); } } if (hit.collider != null) { landMarks.Clear(); if (hit.collider.gameObject != lastLand) { CloseLandMark(); } currentLand = hit.collider.gameObject; if (currentLand == lastLand) return; ShowLandMark(); } else { CloseLandMark(); } } /// /// 展示地标 /// /// public async void ShowLandMark() { if (currentLand == null) return; currentDistCode = currentLand.GetComponent().distCode; currentLand.GetComponent().materials = select; Vector3 worldToScreenPoint = Camera.main.WorldToScreenPoint(new Vector3(currentLand.transform.position.x, currentLand.transform.position.y, currentLand.transform.position.z)); lastLand = currentLand; SwitchLevel(-1); AreaData areaData = await AsyncWebReq.Get($"{urlDic["地图弹出框"]}{currentDistCode}", webAdapter.head, webAdapter.token); if (!bool.Parse(areaData.success)) return; for (int i = 0; i < areaData.data.items.Count; i++) { string info = $"{areaData.data.items[i].label}:{areaData.data.items[i].value}{areaData.data.items[i].unit}"; landMarks.Add(info); } landMarkAndInfoCotroller.gameObject.SetActive(true); landMarkAndInfoCotroller.GetComponent().position = new Vector3(worldToScreenPoint.x, worldToScreenPoint.y, 0); if (currentLand != null) landMarkAndInfoCotroller.SetMarksInfo(currentLand.name, landMarks); } /// /// 关闭地标 /// public void CloseLandMark() { landMarkAndInfoCotroller.gameObject.SetActive(false); if (lastLand != null) { lastLand.GetComponent().materials = mat; lastLand = null; } currentLand = null; SwitchLevel(currentLevel); } /// /// 2 省会 1 城市 0 区县 /// public void SwitchLand(int _currentLevel) { if (currentLand == null) return; webAdapter.LevelChange(_currentLevel.ToString()); landMarkAndInfoCotroller.gameObject.SetActive(false); currentLevel = _currentLevel; clickCurrentLevel = _currentLevel; shadowMap.gameObject.SetActive(false); switch (currentLevel) { case 2: //Debug.Log("省会"); for (int i = 0; i < cityParents.childCount; i++) { cityParents.GetChild(i).gameObject.SetActive(false); } lastCity = null; shadowMap.gameObject.SetActive(true); SwitchMatShow(provincialCapital); break; case 1: //Debug.Log("城市"); if (lastCity) { if (currentArea != null) currentArea.SetActive(false); SwitchMatShow(lastCity); lastCity = null; } else { for (int i = 0; i < cityParents.childCount; i++) { var child = cityParents.GetChild(i).gameObject; child.SetActive(child.name == currentLand.name); } SwitchMatHide(provincialCapital); } break; case 0: //lastCity = currentLand.transform.parent; //SwitchMatHide(lastCity); //for (int i = 0; i < areaParents.childCount; i++) //{ // var child = areaParents.GetChild(i).gameObject; // child.SetActive(child.name == currentLand.name); // if (child.activeSelf) // { // currentArea = child; // } //} Debug.Log("区县"); break; } } public void SwitchMatShow(Transform parent) { if (_fadeTweens.TryGetValue(parent, out var tween)) { tween.Kill(true); _fadeTweens.Remove(parent); } //bool isChange = true; var renderers = GetMeshRenderers(parent); List currentMaters = new List(); for (int i = 0; i < renderers.Count; i++) { renderers[i].gameObject.SetActive(true); for (int j = 0; j < renderers[i].materials.Length; j++) { currentMaters.Add(renderers[i].materials[j]); } } tween = DOVirtual.Float(1, 0, 0.5f, t => { for (int i = 0; i < currentMaters.Count; i++) { currentMaters[i].SetFloat("_Opacity", t); } }).OnComplete(() => { for (int i = 0; i < renderers.Count; i++) { renderers[i].GetComponent().enabled = true; renderers[i].materials = mat; } _fadeTweens.Remove(parent); }); _fadeTweens.Add(parent, tween); } private void SwitchMatHide(Transform parent) { if (_fadeTweens.TryGetValue(parent, out var tween)) { tween.Kill(true); _fadeTweens.Remove(parent); } var renderers = GetMeshRenderers(parent); for (int i = 0; i < renderers.Count; i++) { renderers[i].GetComponent().enabled = false; renderers[i].materials = opacity; } List currentMaters = new List(); for (int i = 0; i < renderers.Count; i++) { for (int j = 0; j < renderers[i].materials.Length; j++) { currentMaters.Add(renderers[i].materials[j]); } } tween = DOVirtual.Float(0, 1, 0.5f, t => { for (int i = 0; i < currentMaters.Count; i++) { currentMaters[i].SetFloat("_Opacity", t); } }).OnComplete(() => { for (int i = 0; i < renderers.Count; i++) { renderers[i].gameObject.SetActive(false); } _fadeTweens.Remove(parent); }); _fadeTweens.Add(parent, tween); } private List GetMeshRenderers(Transform t) { List renderers = new List(); for (int i = 0; i < t.childCount; i++) { renderers.Add(t.GetChild(i).GetComponent()); } return renderers; } private void SwitchLevel(int currentLevel) { switch (currentLevel) { case -1: cameraRt.SetMaxMinDistance(30f, 120f); break; case 0: cameraRt.SetMaxMinDistance(10f, 20f); break; case 1: cameraRt.SetMaxMinDistance(30f, 60f); break; case 2: cameraRt.SetMaxMinDistance(70f, 120f); break; } } }