batteryDiagnosis/Assets/Scripts/UI/UIPanel/UI_MiddleTipPanel.cs

71 lines
2.3 KiB
C#

using System.Collections;
using TMPro;
using UnityEngine;
public class UI_MiddleTipPanel : BasePanel
{
/// <summary>
/// 面板显示后几秒后消失
/// </summary>
private float disappear = 5f;
public TextMeshProUGUI text_Content;
public TextMeshProUGUI text_Title;
public RectTransform bg;
/// <summary>
/// 初始化
/// </summary>
public void Init(string content,float disappear_)
{
text_Title.text = content;
text_Content.text = "";
disappear = disappear_;
AdjustImageWidth(text_Title, bg, 30, 21);
StartCoroutine(TypeWriterEffect(content));
StopCoroutine(HideAsync());
StartCoroutine(HideAsync());
}
/// <summary>
/// 打字机效果
/// </summary>
private IEnumerator TypeWriterEffect(string text)
{
yield return new WaitForSeconds(0.1f);
text_Content.text = "";
float textLength = text.Length;
for (int i = 0; i < textLength; i++)
{
text_Content.text += text[i];
yield return new WaitForSeconds(0.05f);
}
}
public override void ShowMe()
{
base.ShowMe();
//StopCoroutine(HideAsync());
}
public override void HideMe()
{
base.HideMe();
StopAllCoroutines();
}
private IEnumerator HideAsync()
{
yield return new WaitForSeconds(disappear);
if (Bootstrap.UIMgr.GetPanel<UI_MiddleTipPanel>()&& disappear!=0)
Bootstrap.UIMgr.HidePanel<UI_MiddleTipPanel>();
}
/// <summary>
/// 根据文字数量改变 背景 大小
/// </summary>
/// <param name="contentText"></param>
public static void AdjustImageWidth(TextMeshProUGUI contentText, RectTransform _bg, float width, float height)
{
float preferredWidth = contentText.preferredWidth;
float preferredHeight = contentText.preferredHeight;
_bg.sizeDelta = new Vector2(preferredWidth + (contentText.text == "" ? 0 : width), _bg.sizeDelta.y);
//_bg.sizeDelta = new Vector2(preferredWidth + (contentText.text == "" ? 0 : width), preferredHeight + (contentText.text == "" ? 0 : height)+20);
//contentText.GetComponent<RectTransform>().sizeDelta = new Vector2(preferredWidth + (contentText.text == "" ? 0 : width), height);
}
}