595 lines
18 KiB
C#
595 lines
18 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using AFeijia.UI;
|
||
using TMPro;
|
||
using UnityEngineInternal;
|
||
using System.Net;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Text;
|
||
|
||
public class CanvasMain : UITool
|
||
{
|
||
[Manual]
|
||
public static CanvasMain Instance { get; private set; }
|
||
|
||
#region 交互
|
||
/// <summary>
|
||
/// 退出按钮
|
||
/// </summary>
|
||
private Button QuitBtn;
|
||
/// <summary>
|
||
/// 教学模式
|
||
/// </summary>
|
||
private Button TutorialBtn;
|
||
/// <summary>
|
||
/// 练习模式
|
||
/// </summary>
|
||
private Button TrainingBtn;
|
||
/// <summary>
|
||
/// 考核模式
|
||
/// </summary>
|
||
private Button ExaminationBtn;
|
||
|
||
/// <summary>
|
||
/// 原理按钮
|
||
/// </summary>
|
||
private Button PrincipleBtn;
|
||
/// <summary>
|
||
/// 结构按钮
|
||
/// </summary>
|
||
private Button StructureBtn;
|
||
/// <summary>
|
||
/// True:原理模式;False:结构模式
|
||
/// 默认为原理模式
|
||
/// </summary>
|
||
[Manual]
|
||
public bool IsPrincipleMode = true;
|
||
/// <summary>
|
||
/// True:教学模式;False:练习模式
|
||
/// </summary>
|
||
[Manual]
|
||
public bool IsTutorialMode = true;
|
||
#endregion
|
||
|
||
#region 用户信息
|
||
/// <summary>
|
||
/// 用户姓名
|
||
/// </summary>
|
||
private TextMeshProUGUI UserName;
|
||
/// <summary>
|
||
/// 用户部门信息
|
||
/// </summary>
|
||
private TextMeshProUGUI UserDepartment;
|
||
/// <summary>
|
||
/// 用户登录时间
|
||
/// </summary>
|
||
private TextMeshProUGUI UserLoginTime;
|
||
|
||
/// <summary>
|
||
/// 用户信息数据
|
||
/// </summary>
|
||
[Manual]
|
||
public UserInfoData UserInfoData;
|
||
#endregion
|
||
|
||
#region 分类、设备信息
|
||
/// <summary>
|
||
/// 分类容器
|
||
/// </summary>
|
||
private RectTransform ClassificationContent;
|
||
/// <summary>
|
||
/// 分类数据
|
||
/// </summary>
|
||
[Manual]
|
||
public ClassificationData[] ClassificationData;
|
||
/// <summary>
|
||
/// 分类预制体
|
||
/// </summary>
|
||
[Manual]
|
||
public ClassificationItem ClassificationButtonPrefab;
|
||
|
||
/// <summary>
|
||
/// 设备信息介绍部分根节点
|
||
/// </summary>
|
||
public RectTransform DeviceIntroductionRoot;
|
||
/// <summary>
|
||
/// 设备信息介绍
|
||
/// </summary>
|
||
public TextMeshProUGUI Introduction;
|
||
|
||
/// <summary>
|
||
/// 播放配音按钮
|
||
/// </summary>
|
||
public Button PlayAudioButton;
|
||
|
||
[Manual]
|
||
public Canvas TagCanvas;
|
||
/// <summary>
|
||
/// 结构标签容器
|
||
/// </summary>
|
||
[Manual]
|
||
public RectTransform TagsContent;
|
||
/// <summary>
|
||
/// 上一个选中的设备
|
||
/// </summary>
|
||
[Manual]
|
||
public DeviceItem LastDeviceItem;
|
||
/// <summary>
|
||
/// 实例化的标签
|
||
/// </summary>
|
||
[Manual]
|
||
public List<TagFollow> TagFollows = new List<TagFollow>();
|
||
#endregion
|
||
|
||
#region 界面控制节点
|
||
|
||
/// <summary>
|
||
/// 设备生成根节点
|
||
/// </summary>
|
||
public GameObject GameCenter;
|
||
/// <summary>
|
||
/// 设备分类列表根节点
|
||
/// </summary>
|
||
public RectTransform DeviceListRoot;
|
||
/// <summary>
|
||
/// 训练界面根节点
|
||
/// </summary>
|
||
public RectTransform TrainingRoot;
|
||
/// <summary>
|
||
/// 考核界面根节点
|
||
/// </summary>
|
||
public RectTransform ExaminationRoot;
|
||
/// <summary>
|
||
/// 教学交互根节点
|
||
/// </summary>
|
||
public RectTransform TutorialRoot;
|
||
|
||
#endregion
|
||
|
||
#region 数据
|
||
public ExamInfo ExamInfo;
|
||
public List<ExamContent> ExamContents = new List<ExamContent>();
|
||
#endregion
|
||
double t;
|
||
protected override void Awake()
|
||
{
|
||
Debug.Log(t = Time.realtimeSinceStartupAsDouble);
|
||
base.Awake();
|
||
Instance = this;
|
||
Debug.Log(Time.realtimeSinceStartupAsDouble - t);
|
||
}
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
if (GameInit.Instance != null)
|
||
{
|
||
switch (GameInit.GameMode)
|
||
{
|
||
case GameMode.Tutorial:
|
||
OnTutorial();
|
||
break;
|
||
case GameMode.Training:
|
||
OnTraining();
|
||
break;
|
||
case GameMode.Examination:
|
||
//切换到考核模式
|
||
//考核模式沿用训练功能,做部分适配
|
||
OnExamination();
|
||
GetExamInfo();
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//在编辑器启动
|
||
InterfaceManager.IpAddress = "http://localhost:5204/";
|
||
}
|
||
Introduction.text = "";
|
||
|
||
QuitBtn.onClick.AddListener(OnQuit);
|
||
|
||
TutorialBtn.onClick.AddListener(OnTutorial);
|
||
TrainingBtn.onClick.AddListener(OnTraining);
|
||
|
||
PrincipleBtn.onClick.AddListener(OnPrinciple);
|
||
StructureBtn.onClick.AddListener(OnStructure);
|
||
|
||
PlayAudioButton.onClick.AddListener(OnPlayAudio);
|
||
|
||
//接口调用
|
||
UpdateClassificationData();
|
||
UpdateUserInfo();
|
||
//GetExamInfo();
|
||
|
||
StartCoroutine(MainCoroutine.Instance.CallLogin());
|
||
}
|
||
|
||
private void LateUpdate()
|
||
{
|
||
if (hide)
|
||
{
|
||
cal += 1;
|
||
if (cal >= 1)
|
||
{
|
||
go.gameObject.SetActive(false);
|
||
hide = false;
|
||
}
|
||
}
|
||
#if UNITY_EDITOR
|
||
if (Input.GetKeyDown(KeyCode.Space))
|
||
{
|
||
OnExamination();
|
||
}
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 点击退出按钮
|
||
/// </summary>
|
||
private void OnQuit()
|
||
{
|
||
//弹出面板时禁用实时效果
|
||
OrbitCamera.ProhibitOperations = true;
|
||
TipsPanel.Instance.Tips("提示", "确定要退出吗?", (quit) =>
|
||
{
|
||
if (quit)
|
||
{
|
||
Application.Quit();
|
||
}
|
||
else
|
||
{
|
||
//面板关闭时恢复
|
||
OrbitCamera.ProhibitOperations = false;
|
||
}
|
||
});
|
||
//#if UNITY_EDITOR
|
||
// Debug.Log("退出");
|
||
//#else
|
||
// Application.Quit();
|
||
//#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 点击教学模式按钮
|
||
/// </summary>
|
||
private void OnTutorial()
|
||
{
|
||
Debug.Log("教学模式");
|
||
IsTutorialMode = true;
|
||
//修改按钮图标状态
|
||
TutorialBtn.GetComponent<CusButton>().SetStatus(true);
|
||
TrainingBtn.GetComponent<CusButton>().SetStatus(false);
|
||
//通过相机渲染层来控制展示模型
|
||
Camera.main.cullingMask |= (1 << 8);
|
||
//恢复标签展示
|
||
TagCanvas.gameObject.SetActive(true);
|
||
//恢复相机旋转平移缩放等操作
|
||
OrbitCamera.ProhibitOperations = false;
|
||
//展示教学界面
|
||
TutorialRoot.gameObject.SetActive(true);
|
||
//隐藏训练界面
|
||
TrainingRoot.gameObject.SetActive(false);
|
||
//提交训练成绩会隐藏设备列表,在此恢复显示设备列表
|
||
DeviceListRoot.gameObject.SetActive(true);
|
||
//
|
||
TrainingManager.Instance.IsTutorial = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 点击练习模式按钮
|
||
/// </summary>
|
||
private void OnTraining()
|
||
{
|
||
Debug.Log("练习模式");
|
||
IsTutorialMode = false;
|
||
//修改按钮图标状态
|
||
TutorialBtn.GetComponent<CusButton>().SetStatus(false);
|
||
TrainingBtn.GetComponent<CusButton>().SetStatus(true);
|
||
//通过相机渲染层来控制不展示模型
|
||
Camera.main.cullingMask &= ~(1 << 8);
|
||
//不展示标签
|
||
TagCanvas.gameObject.SetActive(false);
|
||
//禁止相机旋转平移缩放等操作
|
||
OrbitCamera.ProhibitOperations = true;
|
||
//隐藏设备介绍信息
|
||
TutorialRoot.gameObject.SetActive(false);
|
||
//展示训练界面
|
||
TrainingRoot.gameObject.SetActive(true);
|
||
//根据所选设备初始化选择项
|
||
TrainingManager.Instance.Init(DeviceItem.SelectedItem);
|
||
//若上次提交成绩后未返回,则再次进入练习模式时需要隐藏设备列表
|
||
if (TrainingManager.Instance.TrainingScore.gameObject.activeSelf)
|
||
DeviceListRoot.gameObject.SetActive(false);
|
||
//开始训练计时
|
||
MainCoroutine.Instance.StartTimer();
|
||
//
|
||
TrainingManager.Instance.IsTutorial = false;
|
||
//关闭解说
|
||
AudioManager.Instance.StopAudio();
|
||
//状态恢复
|
||
PlayAudioButton.GetComponent<CusButton>().SetStatus(false);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 考核模式
|
||
/// </summary>
|
||
private void OnExamination()
|
||
{
|
||
//隐藏教学和练习按钮,展示考核按钮
|
||
TutorialBtn.gameObject.SetActive(false);
|
||
TrainingBtn.gameObject.SetActive(false);
|
||
ExaminationBtn.gameObject.SetActive(true);
|
||
//通过相机渲染层来控制不展示模型
|
||
Camera.main.cullingMask &= ~(1 << 8);
|
||
//不展示标签
|
||
TagCanvas.gameObject.SetActive(false);
|
||
//禁止相机旋转平移缩放等操作
|
||
OrbitCamera.ProhibitOperations = true;
|
||
//隐藏设备介绍信息
|
||
TutorialRoot.gameObject.SetActive(false);
|
||
//展示考核界面
|
||
TrainingRoot.gameObject.SetActive(true);
|
||
//切换考核界面
|
||
TrainingManager.Instance.SwitchExaminationMode();
|
||
|
||
TrainingManager.Instance.IsTutorial = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 点击原理按钮
|
||
/// </summary>
|
||
public void OnPrinciple()
|
||
{
|
||
Debug.Log("原理");
|
||
|
||
//切换回原理模式,关闭爆炸效果
|
||
if (!IsPrincipleMode)
|
||
{
|
||
PrincipleBtn.image.color = new Color(1, 1, 1, 1);
|
||
StructureBtn.image.color = new Color(1, 1, 1, 0.4f);
|
||
|
||
DeviceIntroductionRoot.gameObject.SetActive(true);
|
||
|
||
HideDeviceTags();
|
||
IsPrincipleMode = true;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 点击结构按钮
|
||
/// 展示标签信息
|
||
/// 标签信息在点击结构按钮和在结构菜单下切换设备都需要改变
|
||
/// </summary>
|
||
private void OnStructure()
|
||
{
|
||
Debug.Log("结构");
|
||
if (IsPrincipleMode)
|
||
{
|
||
StructureBtn.image.color = new Color(1, 1, 1, 1);
|
||
PrincipleBtn.image.color = new Color(1, 1, 1, 0.4f);
|
||
|
||
DeviceIntroductionRoot.gameObject.SetActive(false);
|
||
ShowDeviceTags();
|
||
|
||
IsPrincipleMode = false;
|
||
}
|
||
}
|
||
|
||
private void OnPlayAudio()
|
||
{
|
||
if (AudioManager.Instance.IsPlaying())
|
||
{
|
||
AudioManager.Instance.PauseAudio();
|
||
PlayAudioButton.GetComponent<CusButton>().SetStatus(false);
|
||
}
|
||
else
|
||
{
|
||
if (DeviceItem.SelectedItem != null)
|
||
{
|
||
AudioManager.Instance.PlayAudio(DeviceItem.SelectedItem.DevicesItemData.dev_name, true);
|
||
PlayAudioButton.GetComponent<CusButton>().SetStatus(true);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 展示设备标签及爆炸效果
|
||
/// </summary>
|
||
public void ShowDeviceTags()
|
||
{
|
||
//根据设备展示标签
|
||
if (DeviceItem.SelectedItem == null) return;
|
||
//所选设备没变,无需更新展示内容
|
||
if (LastDeviceItem != null && LastDeviceItem == DeviceItem.SelectedItem) return;
|
||
|
||
//所选设备改变,需要更新展示内容
|
||
for (int i = 0; i < TagFollows.Count; i++)
|
||
{
|
||
Destroy(TagFollows[i].gameObject);
|
||
}
|
||
TagFollows.Clear();
|
||
|
||
if (!DeviceItem.InstantiatedObjects.ContainsKey(DeviceItem.SelectedItem.DevicesItemData.dev_name))
|
||
{
|
||
Debug.Log("未找到对应设备模型!");
|
||
return;
|
||
}
|
||
var selectedDevice = DeviceItem.InstantiatedObjects[DeviceItem.SelectedItem.DevicesItemData.dev_name];
|
||
|
||
//展示爆炸动画
|
||
if (selectedDevice.transform.GetComponent<Animator>())
|
||
selectedDevice.transform.GetComponent<Animator>().SetTrigger("Explode");
|
||
|
||
for (int i = 0; i < selectedDevice.transform.childCount; i++)
|
||
{
|
||
switch (selectedDevice.transform.GetChild(i).tag)
|
||
{
|
||
case "Tag_L":
|
||
var tag_l = Instantiate(Resources.Load<TagFollow>("Prefabs/UI/Tag_L"), TagsContent);
|
||
tag_l.Init(selectedDevice.transform.GetChild(i).name, selectedDevice.transform.GetChild(i).transform);
|
||
|
||
TagFollows.Add(tag_l);
|
||
break;
|
||
case "Tag_R":
|
||
var tag_r = Instantiate(Resources.Load<TagFollow>("Prefabs/UI/Tag_R"), TagsContent);
|
||
tag_r.Init(selectedDevice.transform.GetChild(i).name, selectedDevice.transform.GetChild(i).transform);
|
||
|
||
TagFollows.Add(tag_r);
|
||
break;
|
||
case "Tag_LB":
|
||
var tag_lb = Instantiate(Resources.Load<TagFollow>("Prefabs/UI/Tag_LB"), TagsContent);
|
||
tag_lb.Init(selectedDevice.transform.GetChild(i).name, selectedDevice.transform.GetChild(i).transform);
|
||
|
||
TagFollows.Add(tag_lb);
|
||
break;
|
||
case "Tag_RB":
|
||
var tag_rb = Instantiate(Resources.Load<TagFollow>("Prefabs/UI/Tag_RB"), TagsContent);
|
||
tag_rb.Init(selectedDevice.transform.GetChild(i).name, selectedDevice.transform.GetChild(i).transform);
|
||
|
||
TagFollows.Add(tag_rb);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
if (selectedDevice.transform.GetChild(i).CompareTag("Tag_L"))
|
||
{
|
||
var tag = Instantiate(Resources.Load<TagFollow>("Prefabs/UI/Tag_L"), TagsContent);
|
||
tag.Init(selectedDevice.transform.GetChild(i).name, selectedDevice.transform.GetChild(i).transform);
|
||
|
||
TagFollows.Add(tag);
|
||
}
|
||
else if (selectedDevice.transform.GetChild(i).CompareTag("Tag_R"))
|
||
{
|
||
var tag = Instantiate(Resources.Load<TagFollow>("Prefabs/UI/Tag_R"), TagsContent);
|
||
tag.Init(selectedDevice.transform.GetChild(i).name, selectedDevice.transform.GetChild(i).transform);
|
||
|
||
TagFollows.Add(tag);
|
||
}
|
||
}
|
||
TagsContent.gameObject.SetActive(true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 控制上一个爆炸的设备被切换后延迟隐藏
|
||
/// </summary>
|
||
bool hide;
|
||
/// <summary>
|
||
/// 延迟计算时间
|
||
/// </summary>
|
||
float cal;
|
||
/// <summary>
|
||
/// 上一个爆炸设备
|
||
/// </summary>
|
||
GameObject go;
|
||
|
||
/// <summary>
|
||
/// 隐藏标签及爆炸效果
|
||
/// </summary>
|
||
public void HideDeviceTags(bool immediately = false, GameObject lastDevice = null)
|
||
{
|
||
if (DeviceItem.SelectedItem == null) return;
|
||
|
||
if (!DeviceItem.InstantiatedObjects.ContainsKey(DeviceItem.SelectedItem.DevicesItemData.dev_name))
|
||
{
|
||
Debug.Log("未找到对应设备模型!");
|
||
return;
|
||
}
|
||
var selectedDevice = DeviceItem.InstantiatedObjects[DeviceItem.SelectedItem.DevicesItemData.dev_name];
|
||
if (selectedDevice.transform.GetComponent<Animator>())
|
||
{
|
||
if (immediately)
|
||
{
|
||
this.go = selectedDevice;
|
||
selectedDevice.transform.GetComponent<Animator>().Play("收拢", 0, 1);
|
||
hide = true;
|
||
cal = 0;
|
||
}
|
||
else
|
||
{
|
||
selectedDevice.transform.GetComponent<Animator>().SetTrigger("Retract");
|
||
}
|
||
}
|
||
|
||
TagsContent.gameObject.SetActive(false);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新分类设备数据并实例化结构树及设备
|
||
/// </summary>
|
||
private void UpdateClassificationData()
|
||
{
|
||
//StartCoroutine(InterfaceManager.GetData(InterfaceManager.GET_Data, (data) =>
|
||
//{
|
||
// ClassificationData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassificationData[]>(data);
|
||
|
||
// if (ClassificationButtonPrefab == null) ClassificationButtonPrefab = Resources.Load<ClassificationItem>("Prefabs/UI/ClassificationItem");
|
||
|
||
// for (int i = 0; i < ClassificationData.Length; i++)
|
||
// {
|
||
// var inst = Instantiate(ClassificationButtonPrefab, ClassificationContent);
|
||
// inst.Init(ClassificationData[i]);
|
||
// }
|
||
//}));
|
||
|
||
StartCoroutine(InterfaceManager.GetDataFullUrl(InterfaceManager.ConfigureDevicePath, (data) =>
|
||
{
|
||
ClassificationData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassificationData[]>(data);
|
||
|
||
if (ClassificationButtonPrefab == null) ClassificationButtonPrefab = Resources.Load<ClassificationItem>("Prefabs/UI/ClassificationItem");
|
||
|
||
for (int i = 0; i < ClassificationData.Length; i++)
|
||
{
|
||
var inst = Instantiate(ClassificationButtonPrefab, ClassificationContent);
|
||
inst.Init(ClassificationData[i]);
|
||
}
|
||
}));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理用户信息
|
||
/// </summary>
|
||
public void UpdateUserInfo()
|
||
{
|
||
UserName.text = WWW.UnEscapeURL(GameInit.InfoIni.user_name, Encoding.UTF8);
|
||
UserDepartment.text = "--";
|
||
UserLoginTime.text = DateTime.Now.ToString("g");
|
||
//StartCoroutine(InterfaceManager.GetData(InterfaceManager.GET_User, (data) =>
|
||
//{
|
||
// UserInfoData = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfoData>(data);
|
||
|
||
// UserName.text = UserInfoData.name;
|
||
// UserDepartment.text = UserInfoData.department;
|
||
// UserLoginTime.text = UserInfoData.logintime;
|
||
//}));
|
||
}
|
||
|
||
private void GetExamInfo()
|
||
{
|
||
var _exam_info_url = InterfaceManager.API_DIC["IPADDRESS"] + InterfaceManager.API_DIC["EXAM_INFO"] + "?id=" + GameInit.InfoIni.exam_id;
|
||
Debug.Log("-- 获取考试信息url:" + _exam_info_url);
|
||
StartCoroutine(InterfaceManager.Get(_exam_info_url, (_error, _data) =>
|
||
{
|
||
if (!string.IsNullOrEmpty(_error))
|
||
{
|
||
//TipsPanel.Instance.Tips("ERROR", _error, (_true) => { });
|
||
Debug.Log("--" + _error);
|
||
}
|
||
else
|
||
{
|
||
ExamInfo = JsonConvert.DeserializeObject<RequestBase<ExamInfo>>(_data).data;
|
||
ExamContents = JsonConvert.DeserializeObject<List<ExamContent>>(ExamInfo.examContent);
|
||
for (int i = 0; i < ExamContents.Count; i++)
|
||
{
|
||
ExamContents[i].device = ExamContents[i].point.Replace("选择正确的模型图", "");
|
||
}
|
||
}
|
||
}));
|
||
}
|
||
}
|