using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class FreePanelManager : MonoBehaviour
{
[Header("测试")]
public Sprite TestImage;
[Header("面板")]
//特殊情况弹窗
public GameObject TipPanel;
public GameObject AccidentIPanel;
[Header("图像")]
//特殊情况弹窗背景图
public Image TipBg;
//事故点截图
public Image AccidentImage;
[Header("文本")]
//特殊情况弹窗文本
public Text TipText;
[Header("一些奇奇怪怪的东西")]
#region 特殊情况淡入淡出效果
public float fadeInDuration = 1f; // 淡出时间
public float fadeOutDuration = 1f; // 淡出持续时间
public float displayDuration = 2f; // 2秒后自己淡出
private bool isFadingIn = true;
private bool isFadingOut = false;
// 指示文本当前是否正在淡入或淡出
private bool isFading = false;
private bool isFadeEnd = false;
// 运行中的淡入淡出协程引用
private Coroutine fadeCoroutine;
#endregion
public void Update()
{
#region 测试
if (Input.GetKeyDown("j"))
{
// PopAccident(TestImage);
}
#endregion
if (isFadeEnd)
{
StopCoroutine(FadeInOutRoutine());
isFadeEnd = false;
}
}
///
/// 打开特殊情况弹窗
///
public void PopTipPanel()
{
TipPanel.SetActive(true);
StopCoroutine(FadeInOutRoutine());
}
///
/// 弹出事故框框并且修改图片
///
/// 传入一张事故图片(Spite)
public void PopAccident(Sprite image)
{
AccidentImage.sprite=image;
AccidentIPanel.SetActive(true);
}
///
/// 提示面板淡入淡出的携程
///
///
IEnumerator FadeInOutRoutine()
{
isFading = true; // 设置正在淡入淡出标志
//// 淡入
float elapsedTime = 0f;
while (elapsedTime < fadeInDuration)
{
elapsedTime += Time.deltaTime;
//Color textColor = TipText.color;
//textColor.a = elapsedTime / fadeInDuration; // 根据时间计算alpha值
//TipText.color = textColor;
//TipBg.color = textColor;
yield return null; // 等待下一帧
}
// 显示文本一段时间
yield return new WaitForSeconds(displayDuration);
// 淡出
elapsedTime = 0f;
while (elapsedTime < fadeOutDuration)
{
elapsedTime += Time.deltaTime;
Color textColor = TipText.color;
textColor.a = 1f - (elapsedTime / fadeOutDuration); // 根据时间计算alpha值
TipText.color = textColor;
TipBg.color = textColor;
yield return null; // 等待下一帧
}
isFading = false; // 重置正在淡入淡出标志
isFadeEnd = true;//这个携程结束的标志
TipPanel.SetActive(false);
}
}