ShanDongVirtualPowerPlant/u3d-ShanDongVirtualPowerPlant/Assets/Adam/Scripts/Bootstrap.cs

265 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public enum CurrentLevel
{
/// <summary>
/// 省会
/// </summary>
ProvincialCapital,
/// <summary>
/// 城市
/// </summary>
City,
/// <summary>
/// 区
/// </summary>
Area
}
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>();
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;
}
/// <summary>
/// 展示地标
/// </summary>
/// <param name="land"></param>
public void ShowLandMark(GameObject land)
{
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()
{
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;
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;
}
}
}