Tz2/Assets/New/Scripts/ScoreShow.cs

92 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = "耗时:" + "<color=#6CFFE7>" + min.ToString() + ":" + sec.ToString() + "</color>";
}
public List<TextMeshProUGUI> textMeshProUGUIs = new List<TextMeshProUGUI>();
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 = "评分:" + "<color=#6CFFE7>" + "优秀" + "</color>";
}
if (score >= 80 && score < 90)
{
pingfen.text = "评分:" + "<color=#FFD700>" + "良好" + "</color>";
}
if (score < 80 && score >= 70)
{
pingfen.text = "评分:" + "<color=#FF6347>" + "一般" + "</color>";
}
if (score < 70 && score >= 60)
{
pingfen.text = "评分:" + "<color=#FF6347>" + "需改进" + "</color>";
}
if (score < 60)
{
pingfen.text = "评分:" + "<color=#FF6347>" + "差" + "</color>";
}
}
/// <summary>
/// 修复版本:正确替换颜色标签内的数字
/// </summary>
public void ReplaceNumberInColorTag(TextMeshProUGUI text, int newNumber, string colorHex = "#6CFFE7")
{
if (text == null) return;
string originalText = text.text;
// 方法1使用 MatchEvaluator 避免 $ 字符问题
string pattern = $@"<color={Regex.Escape(colorHex)}>(\d+)</color>";
string newText = Regex.Replace(originalText, pattern,
match => $"<color={colorHex}>{newNumber}</color>");
text.text = newText;
}
public void OnQuitBtnClick()
{
Application.Quit();
}
}