1118OPSSNew/Assets/Zion/Scripts/manage/NewLogic.cs

178 lines
4.1 KiB
C#
Raw Permalink 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 SK.Framework;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class NewLogic : MonoBehaviour
{
public GameObject firstCam;
public GameObject thirdCam;
public List<Toggle> partTogs = new List<Toggle>();
public TextMeshProUGUI pingfen;
// Start is called before the first frame update
void Start()
{
foreach (var part in partTogs)
{
part.onValueChanged.AddListener((isOn) => OnToggleValueChanged(part, isOn));
}
RandomScore();
RandomTime();
}
/// <summary>
/// Toggle值改变时的回调
/// </summary>
private void OnToggleValueChanged(Toggle toggle, bool isOn)
{
if (isOn)
{
if (toggle.name == "first")
{
ChangeCamToFirst();
}
else
{
ChangeCamToThird();
}
}
}
void LateUpdate()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
}
}
public void ChangeCamToFirst()
{
if (firstCam.gameObject.activeInHierarchy)
return;
firstCam.SetActive(true);
thirdCam.SetActive(false);
}
public void ChangeCamToThird()
{
if (thirdCam.gameObject.activeInHierarchy)
return;
thirdCam.SetActive(true);
firstCam.SetActive(false);
}
/// <summary>
/// 部件展示场景
/// </summary>
public void LoadSceneToNew()
{
SceneLoader.LoadAsync("PartKnow");
}
/// <summary>
/// 返回主场景
/// </summary>
public void ReturnMainScene()
{
SceneLoader.LoadAsync("GameSencePC");
}
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 ReloadScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}