using HighlightingSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/*
* 脚本使用文字颜色来判断目标是否达成
* 格式固定两行
*
*/
public class Test : MonoBehaviour
{
[Tooltip("录音")]
public Button RecodePen;
//切换开关的旋转状态
public int _SwitchChangerStep;
[Header("这里是需要变色的文字")]
public Text text1, text2;
#region 提示栏文字列表
static string[] t1 = new string[] {
"点击录音按钮",
"点击录音按钮",
"点击录音按钮"
};
static string[] t2 = new string[]
{
"点击A",
"点击B",
"点击C"
};
[Header("需要点击的目标物体")]
public GameObject[] TargetGameobject = new GameObject[t1.Length];
#endregion
public int step;
[Header("录音按钮")]
public Button RecodeSubmit;//这个当时做项目要每个环节都有个录音.做其他需求时可以去掉这个
private void OnEnable()
{
TargetGameobject[step].SetActive(true);
Debug.Log(string.Format("<color=yellow>{0}</color>", $"{System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName}脚本启动") + "\n此脚本检测目标");
RecodeSubmit.onClick.AddListener(() => {
RecodePen.interactable = false;
text1.color = Color.green;
});
}
private void Awake()
{
if (t1.Length != t2.Length) Debug.LogError("文字未能一一对应!"); //初始化
text1.color = Color.white;
text2.color = Color.white;
}
void Update()
{
if (step < t1.Length)
{
text1.text = t1[step];
text2.text = t2[step];
}
else
{
text1.text = null; text2.text = null;
enabled = false;//此脚本的工作结束了,初始化
}
if (text1.color == Color.green && text2.color == Color.green)
{
text1.color = Color.white; text2.color = Color.white; RecodePen.interactable = true;
step++;
TargetGameobject[step].SetActive(true);
RecodeSubmit.onClick.AddListener(() => {
text1.color = Color.green;
});
}
//目标体检测,文字变色
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
Physics.Raycast(ray, out raycastHit, 2000);
Debug.Log(raycastHit.transform.name);
//当时是用高亮来确认目标物体的
//如果不加高亮,就对应Targetgameobject[]去
if (raycastHit.transform.gameObject.activeInHierarchy && raycastHit.transform.gameObject.GetComponent<Highlighter>().enabled)
{
raycastHit.transform.gameObject.SetActive(false);
text2.color = Color.green;
}
}
}
}
实现效果: