279 lines
8.2 KiB
C#
279 lines
8.2 KiB
C#
using DG.Tweening;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using Adam;
|
||
|
||
|
||
public class Bootstrap : MonoSingleton<Bootstrap>
|
||
{
|
||
public LandMarksAndInfoController landMarkAndInfoCotroller;
|
||
/// <summary>
|
||
/// 0 省会 1 城市 2 区县
|
||
/// </summary>
|
||
public int currentLevel = 0;
|
||
|
||
public List<string> landMarks;
|
||
/// <summary>
|
||
/// 透明
|
||
/// </summary>
|
||
public Material[] opacity;
|
||
/// <summary>
|
||
/// 选中
|
||
/// </summary>
|
||
public Material[] select;
|
||
/// <summary>
|
||
/// 默认
|
||
/// </summary>
|
||
public Material[] mat;
|
||
/// <summary>
|
||
/// 当前选中板块
|
||
/// </summary>
|
||
//[HideInInspector]
|
||
public GameObject currentLand;
|
||
|
||
public CameraRT cameraRt;
|
||
/// <summary>
|
||
/// 省会
|
||
/// </summary>
|
||
public Transform provincialCapital;
|
||
/// <summary>
|
||
/// 城市
|
||
/// </summary>
|
||
public Transform cityParents;
|
||
/// <summary>
|
||
/// 县级区域
|
||
/// </summary>
|
||
public Transform areaParents;
|
||
|
||
private Transform lastCity;
|
||
|
||
private GameObject currentArea;
|
||
/// <summary>
|
||
/// 城市
|
||
/// </summary>
|
||
//public GameObject[] citys;
|
||
|
||
|
||
private Dictionary<Transform, Tween> _fadeTweens = new Dictionary<Transform, Tween>();
|
||
|
||
public WebAdapter webAdapter;
|
||
|
||
public Dictionary<string, string> urlDic = new Dictionary<string, string>();
|
||
|
||
|
||
private void Awake()
|
||
{
|
||
string[] data = FileUtil.ReadAllLineFromLocal("WebAddress.json");
|
||
for (int i = 0; i < data.Length; i++)
|
||
{
|
||
string[] urlInfo = data[i].Split("=");
|
||
urlDic.Add(urlInfo[0], urlInfo[1]);
|
||
}
|
||
}
|
||
void Start()
|
||
{
|
||
webAdapter = GetComponent<WebAdapter>();
|
||
landMarks = new List<string> { "网络负荷:49.84 kw", "上网负荷:49.84 kw", "削峰负荷:49.84 kw", "填谷负荷:49.84 kw", "发电负荷:49.84 kw" };
|
||
landMarkAndInfoCotroller.gameObject.SetActive(false);
|
||
cameraRt.OnLimitScroll += SwitchLand;
|
||
|
||
}
|
||
public bool isLoad = false;
|
||
|
||
/// <summary>
|
||
/// 展示地标
|
||
/// </summary>
|
||
/// <param name="land"></param>
|
||
public async void ShowLandMark(GameObject land)
|
||
{
|
||
if (isLoad) return;
|
||
landMarks.Clear();
|
||
string distCode = land.GetComponent<CityInfo>().distCode;
|
||
AreaData areaData = await AsyncWebReq.Get<AreaData>($"{urlDic["地图弹出框"]}{distCode}", webAdapter.head, webAdapter.token);
|
||
if (!bool.Parse(areaData.success)) return;
|
||
isLoad = true;
|
||
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);
|
||
}
|
||
|
||
currentLand = land;
|
||
currentLand.GetComponent<MeshRenderer>().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<RectTransform>().position = new Vector3(worldToScreenPoint.x, worldToScreenPoint.y, 0);
|
||
landMarkAndInfoCotroller.SetMarksInfo(currentLand.name, landMarks);
|
||
SwitchLevel(-1);
|
||
}
|
||
/// <summary>
|
||
/// 关闭地标
|
||
/// </summary>
|
||
public void CloseLandMark()
|
||
{
|
||
isLoad = false;
|
||
landMarkAndInfoCotroller.gameObject.SetActive(false);
|
||
if (currentLand != null)
|
||
{
|
||
currentLand.GetComponent<MeshRenderer>().materials = mat;
|
||
currentLand = null;
|
||
}
|
||
SwitchLevel(currentLevel);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 2 省会 1 城市 0 区县
|
||
/// </summary>
|
||
public void SwitchLand(int _currentLevel)
|
||
{
|
||
if (currentLand == null) return;
|
||
webAdapter.LevelChange(_currentLevel.ToString());
|
||
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<MeshRenderer>();
|
||
List<Material> currentMaters = new List<Material>();
|
||
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<Collider>().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<MeshRenderer>();
|
||
for (int i = 0; i < renderers.Length; i++)
|
||
{
|
||
renderers[i].GetComponent<Collider>().enabled = false;
|
||
renderers[i].materials = opacity;
|
||
}
|
||
List<Material> currentMaters = new List<Material>();
|
||
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;
|
||
|
||
}
|
||
}
|
||
|
||
|
||
}
|