MyBook/Assets/script/InfoShow.cs
2025-03-24 10:33:19 +08:00

283 lines
10 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 System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Text.RegularExpressions;
using UnityEngine.Video;
using Unity.VisualScripting;
public class InfoShow : MonoBehaviour, IPointerClickHandler
{
private void OnEnable()
{
}
void Start()
{
// 确保 TMP 组件支持超链接
textMeshProUGUI.text = "";
textMeshProUGUI.richText = true;
textMeshProUGUI.text = "你好啊,我是你的助手小知\n点击课本我们一起来学习吧";
// 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";
}
if (GameManager.Instance.pointStrName == "")
{
return;
}
else if (GameManager.Instance.pointStrName=="船")
{
displayText += ParseClickableText("[国之重器万吨大驱055是什么?]\n");
}
else if (GameManager.Instance.pointStrName == "小木船")
{
displayText += ParseClickableText("[古代木船是如何在今天再次航行的?]\n");
}
else if (GameManager.Instance.pointStrName == "大轮船")
{
displayText += ParseClickableText("[大轮船的内部是怎样的?]\n");
}
else if (GameManager.Instance.pointStrName == "快艇")
{
displayText += ParseClickableText("[什么是无人船艇?]\n");
}
else if (GameManager.Instance.pointStrName == "潜水艇")
{
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);
GameManager.Instance.StopVideoPlayer();
GameManager.Instance.BlackboardOne.gameObject.SetActive(false);
// 这里可以扩展点击逻辑,例如弹出详细信息
AIquestionAnswer.text = infoDictionary[linkId];
}
else if (linkId== "我想进入下一阶段的学习")
{
GameManager.Instance.StopVideoPlayer();
GameManager.Instance.OnePageThree.gameObject.SetActive(true);
GameManager.Instance.Shop.gameObject.SetActive(false);
GameManager.Instance.BlackboardOne.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 == "国之重器万吨大驱055是什么?")
{
GameManager.Instance.showVideoPlayer("wandongdaqu.mp4");
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("guchuanxinghang.mp4");
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("wurenjikanneibujiegou.mp4");
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("wurenchuan.mp4");
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("heqianting.mp4");
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("StartMP4.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;
}
}
Debug.Log(modelTransform.parent.transform);
// 需要旋转的是父对象
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;
}
}