MyBook/Assets/script/InfoShow.cs

213 lines
7.3 KiB
C#
Raw Normal View History

2025-03-17 17:27:32 +08:00
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Text.RegularExpressions;
using UnityEngine.Video;
public class InfoShow : MonoBehaviour, IPointerClickHandler
{
private void OnEnable()
{
}
void Start()
{
// 确保 TMP 组件支持超链接
textMeshProUGUI.text = "";
textMeshProUGUI.richText = true;
textMeshProUGUI.text = "你好啊,我是你的助手小知快翻开课本我们一起来学习吧!";
setModel("1");
}
// 显示字典信息,每个键值作为可点击链接
public void ShowInfo(Dictionary<string, string> dic)
{
infoDictionary = dic; // 复制字典数据
string displayText = "";
foreach (var item in dic)
{
string key = item.Key;
string value = item.Value;
// 生成 TMP 超链接格式
displayText += $"<link=\"{key}\"><color=#FFD700><u>{key}</u></color></link>\n";
}
displayText += ParseClickableText("[船是怎么漂浮在水面上的?]\n");
displayText += ParseClickableText("\n\n\n[我想进入下一阶段的学习]\n");
displayText += ParseClickableText("[我结束今天的学习]\n");
textMeshProUGUI.text = displayText;
RectTransform rectTransform = textMeshProUGUI.transform.parent.GetComponent<RectTransform>();
rectTransform.anchoredPosition += new Vector2(0, 50);// 改变 UI 位置
}
// 处理点击事件
public void OnPointerClick(PointerEventData eventData)
{
// 获取鼠标点击的超链接索引
int linkIndex = TMP_TextUtilities.FindIntersectingLink(textMeshProUGUI, eventData.position, eventData.pressEventCamera);
if (linkIndex != -1)
{
TMP_LinkInfo linkInfo = textMeshProUGUI.textInfo.linkInfo[linkIndex];
string linkId = linkInfo.GetLinkID();
if (infoDictionary.ContainsKey(linkId))
{
Debug.Log($"点击了 {linkId},值为: {infoDictionary[linkId]}");
GameManager.Instance.questionAnswer.gameObject.SetActive(true);
GameManager.Instance.ImgMask.SetActive(true);
// 这里可以扩展点击逻辑,例如弹出详细信息
AIquestionAnswer.text = infoDictionary[linkId];
}
else if (linkId== "我想进入下一阶段的学习")
{
GameManager.Instance.StopVideoPlayer();
GameManager.Instance.OnePageThree.gameObject.SetActive(true);
GameManager.Instance.Shop.gameObject.SetActive(false);
GameManager.Instance.questionAnswer.gameObject.SetActive(false);
GameManager.Instance.ImgMask.SetActive(false);
}
else if (linkId == "我结束今天的学习")
{
GameManager.Instance.StopVideoPlayer();
GameManager.Instance.aIquestionAnswer.SetActive(false);
GameManager.Instance.Robot.SetActive(false);
GameManager.Instance.onePageEnd.gameObject.SetActive(true);
GameManager.Instance.Shop.gameObject.SetActive(false);
GameManager.Instance.questionAnswer.gameObject.SetActive(false);
GameManager.Instance.ImgMask.SetActive(false);
}
else if (linkId == "船是怎么漂浮在水面上的?")
{
GameManager.Instance.Shop.gameObject.SetActive(false);
GameManager.Instance.BlackboardOne.gameObject.SetActive(true);
GameManager.Instance.questionAnswer.gameObject.SetActive(false);
GameManager.Instance.ImgMask.SetActive(false);
}
else if (linkId == "我想进入上一阶段的学习")
{
GameManager.Instance.showVideoPlayer("d544ee389b313a457470fab9b220f981.mp4");
GameManager.Instance.Shop.gameObject.SetActive(false);
textMeshProUGUI.text = ParseClickableText("你好啊,我是你的助手小知\r\n你想了解什么呢?\r\n高亮的部分可以点击哦!\r\n快来跟我一起学习吧\r\n\n\n[我想进入下一阶段的学习]");
GameManager.Instance.onePageOne.gameObject.SetActive(true);
GameManager.Instance.OnePageThree.gameObject.SetActive(false);
GameManager.Instance.questionAnswer.gameObject.SetActive(false);
GameManager.Instance.ImgMask.SetActive(false);
}
}
}
// 解析 [] 内的内容,转换成 <link> 形式
public string ParseClickableText(string rawText)
{
return Regex.Replace(rawText, @"\[(.*?)\]", "<link=$1><color=yellow><u>$1</u></color></link>");
}
[Header("显示文本相关")]
public TextMeshProUGUI textMeshProUGUI; // 绑定 UI 组件
//回答用的画布
public TextMeshProUGUI AIquestionAnswer;
// 存储点击事件的字典
private Dictionary<string, string> infoDictionary = new Dictionary<string, string>();
private GameObject fatherGameObject;
[Header("视频播放相关")]
public VideoPlayer videoPlayer;
public void showVideoPlayer(string path)
{
string fullPath = System.IO.Path.Combine(Application.streamingAssetsPath, path);
videoPlayer.url = fullPath;
// 播放视频
videoPlayer.Play();
}
public void setVideoPositon(Vector2 pos)
{
videoPlayer.transform.position = pos;
}
[Header("3d模型展示相关")]
public ModelRotationHandler modelRotationHandler;
public Camera modelCamera; // 3D模型的摄像机
private Transform modelTransform; // 3D模型的 Transform
public float padding = 2.5f; // 额外边距,防止模型太贴近边界
public GameObject GameObject_r_y;
public GameObject GameObject_r_x;
public GameObject Camera_pos_z;
public List<ModelInfo> modelInfoList = new List<ModelInfo>();
public void setModel(string modelName)
{
// 从预设的模型中找到这个模型
foreach (ModelInfo modelInfo in modelInfoList)
{
if (modelInfo.modelName == modelName)
{
modelTransform = modelInfo.modelTransform;
break;
}
}
// 需要旋转的是父对象
modelRotationHandler.modelTransform = modelTransform.parent.transform;
AdjustCameraToFitModel();
}
// 设置模型画布的位置
public void setModelShowPosition(Vector2 pos)
{
modelRotationHandler.transform.position = pos;
}
public void AdjustCameraToFitModel()
{
GameObject_r_y.transform.position = modelTransform.position;
GameObject_r_y.transform.localRotation = Quaternion.Euler(0, 0, 0);
GameObject_r_x.transform.localRotation = Quaternion.Euler(0, 0, 0);
Camera_pos_z.transform.localPosition = new Vector3(0, 0, -4);
modelRotationHandler.GameObject_r_y = GameObject_r_y;
modelRotationHandler.GameObject_r_x = GameObject_r_x;
modelRotationHandler.Camera_pos_z = Camera_pos_z;
}
[System.Serializable]
public class ModelInfo
{
public string modelName;
public Transform modelTransform;
}
}