_xiaofang/xiaofang/Assets/Script/Manager/PromptMgr.cs

57 lines
1.7 KiB
C#
Raw Normal View History

2024-10-24 18:01:55 +08:00
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;//<2F>ƶ<EFBFBD><C6B6>ľ<EFBFBD><C4BE><EFBFBD>
private float moveDuration = 1f;//<2F>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
// Start is called before the first frame update
void Start()
{
Instance = this;
PromptPrefab = (GameObject)Resources.Load("UIPrefab/PromptBg");
promptpos = GameObject.Find("Canvas/Prompt").GetComponent<Transform>();
}
public void PromptBubble(string message)
{
GameObject proobj = Instantiate(PromptPrefab, promptpos);
StartCoroutine(MoveUpandDestory(proobj));
Text protext = proobj.transform.Find("PromptText").GetComponent<Text>(); ;
protext.text = message;
}
IEnumerator MoveUpandDestory(GameObject obj)
{
RectTransform uielement = obj.GetComponent<RectTransform>();
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; // <20>ȴ<EFBFBD><C8B4><EFBFBD>һ֡
}
uielement.anchoredPosition = endPosition; // ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>׼ȷ
// <20>ȴ<EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
yield return new WaitForSeconds(1f); // <20>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD>ĵȴ<C4B5>ʱ<EFBFBD><CAB1>
Destroy(uielement.gameObject); // <20><><EFBFBD><EFBFBD>UIԪ<49><D4AA>
}
// Update is called once per frame
}