using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using TMPro; using UnityEngine; using UnityEngine.UI; public class ScoreShow : MonoBehaviour { public TextMeshProUGUI pingfen; // Start is called before the first frame update void Start() { RandomScore(); RandomTime(); } public Text timeText; public void RandomTime() { int min = Random.Range(10, 14); int sec = Random.Range(10, 50); timeText.text = "耗时:" + "" + min.ToString() + ":" + sec.ToString() + ""; } public List textMeshProUGUIs = new List(); public TextMeshProUGUI score; private int AllScore; public void RandomScore() { foreach (var text in textMeshProUGUIs) { int result = Random.Range(4, int.Parse(text.name) + 1); AllScore += result; ReplaceNumberInColorTag(text, result); } ReplaceNumberInColorTag(score, AllScore); ShowResult(AllScore); } void ShowResult(int score) { if (score >= 90 && score < 100) { pingfen.text = "评分:" + "" + "优秀" + ""; } if (score >= 80 && score < 90) { pingfen.text = "评分:" + "" + "良好" + ""; } if (score < 80 && score >= 70) { pingfen.text = "评分:" + "" + "一般" + ""; } if (score < 70 && score >= 60) { pingfen.text = "评分:" + "" + "需改进" + ""; } if (score < 60) { pingfen.text = "评分:" + "" + "差" + ""; } } /// /// 修复版本:正确替换颜色标签内的数字 /// public void ReplaceNumberInColorTag(TextMeshProUGUI text, int newNumber, string colorHex = "#6CFFE7") { if (text == null) return; string originalText = text.text; // 方法1:使用 MatchEvaluator 避免 $ 字符问题 string pattern = $@"(\d+)"; string newText = Regex.Replace(originalText, pattern, match => $"{newNumber}"); text.text = newText; } public void OnQuitBtnClick() { Application.Quit(); } }