Cute_demon_attacks/meng_yao/Assets/script/Manager/Promptmgr.cs
2024-11-14 09:57:30 +08:00

97 lines
3.1 KiB
C#

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;
// Start is called before the first frame update
void Start()
{
Instance = this;
DontDestroyOnLoad(this);
PromptPrefab = (GameObject)Resources.Load("Prefabs/Prompt");
PromptWhiltePrefab = (GameObject)Resources.Load("Prefabs/PromptWhite");
}
public void PromptBubble(string message)
{
PromptBubble(message, Color.white, Color.black);
}
/// <summary>
/// 显示弹窗
/// </summary>
/// <param name="message">显示的内容</param>
/// <param name="textColor">文字颜色,默认白色</param>
/// <param name="backgroundColor">背景颜色,默认黑色</param>
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<Text>();
protext.text = message;
protext.color = textColor; // 设置文本颜色
// 设置背景颜色
Image backgroundImage = proobj.GetComponent<Image>();
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<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; // 等待下一帧
}
uielement.anchoredPosition = endPosition; // 确保最终位置准确
// 等待一段时间后销毁
yield return new WaitForSeconds(1f); // 可调整的等待时间
if (uielement != null && uielement.gameObject != null)
{
Destroy(uielement.gameObject); // 销毁UI元素
}
}
}