Cute_demon_attacks/meng_yao/Assets/script/Manager/Promptmgr.cs

104 lines
3.3 KiB
C#
Raw Normal View History

2024-10-28 10:57:19 +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;
2024-10-28 16:41:14 +08:00
private GameObject PromptWhiltePrefab;
2024-10-28 10:57:19 +08:00
private float movedistance = 400f;//<2F>ƶ<EFBFBD><C6B6>ľ<EFBFBD><C4BE><EFBFBD>
private float moveDuration = 1f;//<2F>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2024-10-29 20:47:58 +08:00
public GameObject canvs;
2024-10-29 12:01:30 +08:00
2024-10-28 10:57:19 +08:00
// Start is called before the first frame update
void Start()
{
Instance = this;
DontDestroyOnLoad(this);
PromptPrefab = (GameObject)Resources.Load("Prefabs/Prompt");
2024-10-28 16:41:14 +08:00
PromptWhiltePrefab = (GameObject)Resources.Load("Prefabs/PromptWhite");
2024-10-29 20:47:58 +08:00
2024-10-28 10:57:19 +08:00
}
2024-10-29 12:01:30 +08:00
/* 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<Text>(); ;
protext.text = message;
}*/
2024-10-28 10:57:19 +08:00
public void PromptBubble(string message)
2024-10-29 12:01:30 +08:00
{
PromptBubble(message, Color.white, Color.black);
}
/// <summary>
2024-10-29 15:52:43 +08:00
/// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
2024-10-29 12:01:30 +08:00
/// </summary>
/// <param name="message"><3E><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="textColor"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>Ĭ<EFBFBD>ϰ<EFBFBD>ɫ</param>
/// <param name="backgroundColor"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>Ĭ<EFBFBD>Ϻ<EFBFBD>ɫ</param>
public void PromptBubble(string message, Color textColor, Color backgroundColor)
2024-10-28 10:57:19 +08:00
{
GameObject proobj = Instantiate(PromptPrefab);
2024-10-29 20:47:58 +08:00
proobj.transform.SetParent(canvs.transform);
2024-10-28 10:57:19 +08:00
proobj.transform.position = new Vector3(540, 1300, 0);
StartCoroutine(MoveUpandDestory(proobj));
2024-10-29 12:01:30 +08:00
Text protext = proobj.transform.Find("Prompttext").GetComponent<Text>();
2024-10-28 10:57:19 +08:00
protext.text = message;
2024-10-29 12:01:30 +08:00
protext.color = textColor; // <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ɫ
2024-10-28 10:57:19 +08:00
2024-10-29 12:01:30 +08:00
// <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ɫ
Image backgroundImage = proobj.GetComponent<Image>();
if (backgroundImage != null)
{
backgroundImage.color = backgroundColor; // <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ɫ
}
}
2024-10-28 16:41:14 +08:00
public void PromptWhilteBubble(string message)
{
GameObject proobj = Instantiate(PromptWhiltePrefab);
2024-10-29 20:47:58 +08:00
proobj.transform.SetParent(canvs.transform);
2024-10-28 16:41:14 +08:00
proobj.transform.position = new Vector3(540, 1300, 0);
StartCoroutine(MoveUpandDestory(proobj));
Text protext = proobj.transform.Find("Prompttext").GetComponent<Text>(); ;
protext.text = message;
2024-10-28 10:57:19 +08:00
}
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>
2024-10-28 22:05:14 +08:00
yield return new WaitForSeconds(1f); // <20>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD>ĵȴ<C4B5>ʱ<EFBFBD><CAB1>
2024-10-28 10:57:19 +08:00
Destroy(uielement.gameObject); // <20><><EFBFBD><EFBFBD>UIԪ<49><D4AA>
}
}