修改messagebox显示

另外修改了一些因为之前愚蠢的方案而导致的bug
This commit is contained in:
2024-09-02 18:40:56 +08:00
parent f6318284d3
commit 41df565c03
7 changed files with 1242 additions and 146 deletions
+75 -26
View File
@@ -1,42 +1,91 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Collections;
using Yarn.Unity;
public class MessageBox : MonoBehaviour
{
public TMP_Text messageText; // Unity UI Text component
public void SetWarning(string message)
public static MessageBox Instance { get; private set; }
public TMP_Text state; // 显示状态的TextMeshPro组件
public TMP_Text text; // 显示消息的TextMeshPro组件
private void Awake()
{
messageText.color = Color.red; // Set text color to red for warnings
messageText.text = message;
// 确保单例模式
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
DontDestroyOnLoad(gameObject); // 可选:保持在场景加载之间
}
}
public void SetNormal(string message)
{
messageText.color = Color.black; // Set text color to black for normal messages
messageText.text = message;
}
// Yarn Command处理方法
[YarnCommand("messagebox")]
public void HandleMessageBoxCommand(string type,string text)
public void HandleMessageBoxCommand(string type, string message)
{
// 根据消息类型调用相应的方法
if (type == "warning")
{
SetWarning(text);
switch (type.ToLower())
{
case "warning":
SetWarning(message);
break;
case "normal":
SetNormal(message);
break;
case "null":
SetNull();
break;
default:
Debug.LogError("Invalid message type for messagebox command: " + type);
break;
}
}
else if (type == "normal")
private void Start()
{
SetNormal(text);
}
else
{
Debug.LogError("Invalid message type for messagebox command: " + type);
SetNull();
}
}
// 设置为Warning状态
private void SetWarning(string message)
{
state.text = "WARNING";
state.color = Color.red; // 红色
text.text = message;
text.color = Color.white; // 默认文本颜色
StartCoroutine(FlashWarning()); // 开始闪烁效果
}
// 设置为Normal状态
private void SetNormal(string message)
{
state.text = "NORMAL";
state.color = new Color(1.0f, 0.55f, 0.0f); // 琥珀色
text.text = message;
text.color = Color.white; // 默认文本颜色
StopCoroutine(FlashWarning()); // 停止闪烁效果(如果正在闪烁)
}
// 设置为Null状态
public void SetNull()
{
state.text = "NO SIGNAL";
state.color = Color.blue; // 蓝色
text.text = string.Empty;
text.color = Color.white; // 默认文本颜色
StopCoroutine(FlashWarning()); // 停止闪烁效果(如果正在闪烁)
}
// 闪烁警告效果
private IEnumerator FlashWarning()
{
for (int i = 0; i < 3; i++)
{
state.enabled = false;
yield return new WaitForSeconds(0.5f); // 闪烁间隔
state.enabled = true;
yield return new WaitForSeconds(0.5f); // 闪烁间隔
}
state.enabled = true; // 确保闪烁后状态仍然可见
}
}