using System.Collections; using TMPro; using UnityEngine; public class UI_MiddleTipPanel : BasePanel { /// /// 面板显示后几秒后消失 /// private float disappear = 5f; public TextMeshProUGUI text_Content; public TextMeshProUGUI text_Title; public RectTransform bg; /// /// 初始化 /// 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()); } /// /// 打字机效果协程 /// 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()&& disappear!=0) Bootstrap.UIMgr.HidePanel(); } /// /// 根据文字数量改变 背景 大小 /// /// 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().sizeDelta = new Vector2(preferredWidth + (contentText.text == "" ? 0 : width), height); } }