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 = 400f;//移动的距离 private float moveDuration = 1f;//移动所需的时间 // Start is called before the first frame update void Start() { Instance = this; DontDestroyOnLoad(this); PromptPrefab = (GameObject)Resources.Load("Prefabs/Prompt"); } public void PromptBubble(string message) { GameObject proobj = Instantiate(PromptPrefab); proobj.transform.SetParent(GameObject.Find("Canvas").transform); proobj.transform.position = new Vector3(540, 1300, 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; // 确保最终位置准确 // 等待一段时间后销毁 yield return new WaitForSeconds(0.3f); // 可调整的等待时间 Destroy(uielement.gameObject); // 销毁UI元素 } }