363 lines
11 KiB
C#
363 lines
11 KiB
C#
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<Bootstrap>
|
||
{
|
||
public LandMarksAndInfoController landMarkAndInfoCotroller;
|
||
/// <summary>
|
||
/// 0 省会 1 城市 2 区县
|
||
/// </summary>
|
||
public int currentLevel = 0;
|
||
public string currentDisName;
|
||
public string currentDistCode;
|
||
|
||
private List<string> landMarks = new List<string>();
|
||
/// <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 Dictionary<string, string> urlDic = new Dictionary<string, string>();
|
||
[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<Dictionary<string, string>>(temp);
|
||
//urlDic = JsonUtility.FromJson<Dictionary<string, string>>(temp);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("加载配置文件错误");
|
||
}
|
||
});
|
||
|
||
//string data = FileUtil.ReadFromLocal("WebAddress.json");
|
||
//urlDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
|
||
|
||
webAdapter = GetComponent<WebAdapter>();
|
||
rayHitTester = GetComponent<RayHitTester>();
|
||
}
|
||
public void GetText(string url, Action<string> actionResult)
|
||
{
|
||
StartCoroutine(FileUtil.GetTextAsyn(url, actionResult));
|
||
}
|
||
void Start()
|
||
{
|
||
//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;
|
||
|
||
}
|
||
|
||
|
||
private void Update()
|
||
{
|
||
var hit = rayHitTester.HitTest();
|
||
if (Input.GetMouseButtonDown(2))
|
||
{
|
||
if (hit.collider != null)
|
||
{
|
||
Debug.Log("clickCurrentLevel==" + clickCurrentLevel);
|
||
if (cameraRt.currentLevelIndex == 1) return;
|
||
clickCurrentLevel--;
|
||
if (clickCurrentLevel <= 1)
|
||
{
|
||
clickCurrentLevel = 1;
|
||
}
|
||
cameraRt.currentLevelIndex = clickCurrentLevel;
|
||
//currentLevel = clickCurrentLevel;
|
||
SwitchLand(clickCurrentLevel);
|
||
cameraRt.SetTarget(hit.collider.transform);
|
||
}
|
||
}
|
||
if (hit.collider != null)
|
||
{
|
||
landMarks.Clear();
|
||
if (hit.collider.gameObject != lastLand)
|
||
{
|
||
CloseLandMark();
|
||
}
|
||
currentLand = hit.collider.gameObject;
|
||
if (currentLand == lastLand) return;
|
||
ShowLandMark();
|
||
}
|
||
else
|
||
{
|
||
CloseLandMark();
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 展示地标
|
||
/// </summary>
|
||
/// <param name="land"></param>
|
||
public async void ShowLandMark()
|
||
{
|
||
if (currentLand == null) return;
|
||
currentDistCode = currentLand.GetComponent<CityInfo>().distCode;
|
||
currentLand.GetComponent<MeshRenderer>().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<AreaData>($"{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<RectTransform>().position = new Vector3(worldToScreenPoint.x, worldToScreenPoint.y, 0);
|
||
if (currentLand != null)
|
||
landMarkAndInfoCotroller.SetMarksInfo(currentLand.name, landMarks);
|
||
}
|
||
/// <summary>
|
||
/// 关闭地标
|
||
/// </summary>
|
||
public void CloseLandMark()
|
||
{
|
||
landMarkAndInfoCotroller.gameObject.SetActive(false);
|
||
if (lastLand != null)
|
||
{
|
||
lastLand.GetComponent<MeshRenderer>().materials = mat;
|
||
lastLand = null;
|
||
}
|
||
currentLand = null;
|
||
SwitchLevel(currentLevel);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 2 省会 1 城市 0 区县
|
||
/// </summary>
|
||
public void SwitchLand(int _currentLevel)
|
||
{
|
||
if (currentLand == null) return;
|
||
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;
|
||
}
|
||
webAdapter.OnLevelChange(_currentLevel);
|
||
}
|
||
|
||
|
||
|
||
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<Material> currentMaters = new List<Material>();
|
||
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<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 = GetMeshRenderers(parent);
|
||
for (int i = 0; i < renderers.Count; i++)
|
||
{
|
||
renderers[i].GetComponent<Collider>().enabled = false;
|
||
renderers[i].materials = opacity;
|
||
}
|
||
List<Material> currentMaters = new List<Material>();
|
||
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<MeshRenderer> GetMeshRenderers(Transform t)
|
||
{
|
||
List<MeshRenderer> renderers = new List<MeshRenderer>();
|
||
for (int i = 0; i < t.childCount; i++)
|
||
{
|
||
renderers.Add(t.GetChild(i).GetComponent<MeshRenderer>());
|
||
}
|
||
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;
|
||
|
||
}
|
||
}
|
||
|
||
|
||
}
|