using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Promptmgr : MonoBehaviour { public static Promptmgr Instance; public GameObject PromptPrefab; private GameObject PromptWhiltePrefab; private float movedistance = 400f;//移动的距离 private float moveDuration = 1f;//移动所需的时间 public GameObject canvs; public static AssetsPanel assestPanel; public static Main_AsstePanel Main_AsstePanel; private float timer; // Start is called before the first frame update void Start() { Instance = this; PromptPrefab = (GameObject)Resources.Load("Prefabs/Prompt"); PromptWhiltePrefab = (GameObject)Resources.Load("Prefabs/PromptWhite"); } private void Update() { //timer += Time.deltaTime; //if (timer>3f) //{ // Destroy(this.transform.gameObject); //} } public void PromptBubble(string message) { PromptBubble(message, Color.white, Color.black); } /// /// 显示弹窗 /// /// 显示的内容 /// 文字颜色,默认白色 /// 背景颜色,默认黑色 public void PromptBubble(string message, Color textColor, Color backgroundColor) { GameObject proobj = Instantiate(PromptPrefab); proobj.transform.SetParent(canvs.transform); proobj.transform.position = new Vector3(Screen.width / 2, Screen.height / 2 + 33, 0); StartCoroutine(MoveUpandDestory(proobj)); Text protext = proobj.transform.Find("Prompttext").GetComponent(); protext.text = message; protext.color = textColor; // 设置文本颜色 // 设置背景颜色 Image backgroundImage = proobj.GetComponent(); if (backgroundImage != null) { backgroundImage.color = backgroundColor; // 设置背景颜色 } } public void PromptWhilteBubble(string message) { GameObject proobj = Instantiate(PromptWhiltePrefab); proobj.transform.SetParent(canvs.transform); proobj.transform.position = new Vector3(Screen.width / 2, Screen.height / 2 + 33, 0); StartCoroutine(MoveUpandDestory(proobj)); Text protext = proobj.transform.Find("Prompttext").GetComponent(); ; protext.text = message; } IEnumerator MoveUpandDestory(GameObject obj) { RectTransform uielement = obj.GetComponent(); Vector3 startPosition = uielement.anchoredPosition; Vector3 endPosition = startPosition + new Vector3(0, movedistance, 0); float elapsedTime = 0f; while (elapsedTime < moveDuration) { uielement.anchoredPosition = Vector3.Lerp(startPosition, endPosition, (elapsedTime / moveDuration)); elapsedTime += Time.deltaTime; yield return null; // 等待下一帧 } uielement.anchoredPosition = endPosition; // 确保最终位置准确 if (uielement != null && uielement.gameObject != null) { Destroy(uielement.gameObject); // 销毁UI元素 } } }