using DG.Tweening; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public enum CurrentLevel { /// /// 省会 /// ProvincialCapital, /// /// 城市 /// City, /// /// 区 /// Area } public class Bootstrap : MonoSingleton { public LandMarksAndInfoController landMarkAndInfoCotroller; /// /// 0 省会 1 城市 2 区县 /// public int currentLevel = 0; public List landMarks; /// /// 透明 /// 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(); 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; } /// /// 展示地标 /// /// public void ShowLandMark(GameObject land) { currentLand = land; currentLand.GetComponent().materials = select; landMarkAndInfoCotroller.gameObject.SetActive(true); Vector3 worldToScreenPoint = Camera.main.WorldToScreenPoint(new Vector3(currentLand.transform.position.x, currentLand.transform.position.y, currentLand.transform.position.z)); landMarkAndInfoCotroller.GetComponent().position = new Vector3(worldToScreenPoint.x, worldToScreenPoint.y, 0); landMarkAndInfoCotroller.SetMarksInfo(currentLand.name, landMarks); SwitchLevel(-1); } /// /// 关闭地标 /// public void CloseLandMark() { landMarkAndInfoCotroller.gameObject.SetActive(false); if (currentLand != null) { currentLand.GetComponent().materials = mat; currentLand = null; } SwitchLevel(currentLevel); } /// /// 2 省会 1 城市 0 区县 /// public void SwitchLand(int _currentLevel) { if (currentLand == null) return; landMarkAndInfoCotroller.gameObject.SetActive(false); currentLevel = _currentLevel; switch (currentLevel) { case 2: Debug.Log("省会"); for (int i = 0; i < cityParents.childCount; i++) { cityParents.GetChild(i).gameObject.SetActive(false); } lastCity = null; 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 = parent.GetComponentsInChildren(); List currentMaters = new List(); for (int i = 0; i < renderers.Length; i++) { 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.Length; 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 = parent.GetComponentsInChildren(); for (int i = 0; i < renderers.Length; i++) { renderers[i].GetComponent().enabled = false; renderers[i].materials = opacity; } List currentMaters = new List(); for (int i = 0; i < renderers.Length; 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(() => { _fadeTweens.Remove(parent); }); _fadeTweens.Add(parent, tween); } private void SwitchLevel(int currentLevel) { switch (currentLevel) { case -1: cameraRt.SetMaxMinDistance(10f, 120f); break; case 0: cameraRt.SetMaxMinDistance(10f, 20f); break; case 1: cameraRt.SetMaxMinDistance(30f, 60f); break; case 2: cameraRt.SetMaxMinDistance(70f, 120f); break; } } }