using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PromptMgr : MonoBehaviour { public static PromptMgr Instance; private GameObject PromptPrefab; private Transform promptpos; private float movedistance = 300f;//移动的距离 private float moveDuration = 1f;//移动所需的时间 // Start is called before the first frame update void Start() { Instance = this; PromptPrefab = (GameObject)Resources.Load("UIPrefab/PromptBg"); promptpos = GameObject.Find("Canvas/Prompt").GetComponent(); } public void PromptBubble(string message) { GameObject proobj = Instantiate(PromptPrefab, promptpos); 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; // 确保最终位置准确 // 等待一段时间后销毁 yield return new WaitForSeconds(1f); // 可调整的等待时间 Destroy(uielement.gameObject); // 销毁UI元素 } // Update is called once per frame }