279 lines
7.9 KiB
C#
279 lines
7.9 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Newtonsoft.Json;
|
||
using TMPro;
|
||
using UnityEditor.Rendering.LookDev;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class FreePanelManager : MonoBehaviour
|
||
{
|
||
[Header("测试")]
|
||
public Sprite TestImage;
|
||
[Header("面板")]
|
||
//职业选择面板
|
||
public GameObject OccupationPanel;
|
||
//特殊情况弹窗
|
||
public GameObject TipPanel;
|
||
public GameObject AccidentIPanel;
|
||
//职业介绍框框
|
||
public GameObject OpcTip1;
|
||
public GameObject OpcTip2;
|
||
public GameObject OpcTip3;
|
||
public GameObject OpcTip4;
|
||
public GameObject OpcTip5;
|
||
public GameObject OpcTip6;
|
||
public GameObject OpcTip7;
|
||
public GameObject OpcTip8;
|
||
[Header("图像")]
|
||
//特殊情况弹窗背景图
|
||
public Image TipBg;
|
||
//事故点截图
|
||
public Image AccidentImage;
|
||
[Header("按钮")]
|
||
public Button AccidentButton;
|
||
public Button OpcLeft;
|
||
public Button OpcRight;
|
||
[Header("文本")]
|
||
//特殊情况弹窗文本
|
||
public Text TipText;
|
||
//职业的种类名字
|
||
public Text Ocp1;
|
||
public Text Ocp2;
|
||
public Text Ocp3;
|
||
public Text Ocp4;
|
||
public Text Ocp5;
|
||
public Text Ocp6;
|
||
public Text Ocp7;
|
||
public Text Ocp8;
|
||
[Header("一些奇奇怪怪的东西")]
|
||
#region 特殊情况淡入淡出效果
|
||
public float fadeInDuration = 1f; // 淡出时间
|
||
public float fadeOutDuration = 1f; // 淡出持续时间
|
||
public float displayDuration = 2f; // 2秒后自己淡出
|
||
|
||
private bool isFadingIn = true;
|
||
private bool isFadingOut = false;
|
||
// 指示文本当前是否正在淡入或淡出
|
||
private bool isFading = false;
|
||
private bool isFadeEnd = false;
|
||
// 运行中的淡入淡出协程引用
|
||
private Coroutine fadeCoroutine;
|
||
#endregion
|
||
|
||
#region 创建职业介绍
|
||
public List<GameObject> createdItems = new List<GameObject>();//保存内容用的列表
|
||
public GameObject content1; // Scroll View 的 Content 物体
|
||
public GameObject content2;
|
||
public GameObject content3;
|
||
public GameObject content4;
|
||
public GameObject content5;
|
||
public GameObject content6;
|
||
public GameObject content7;
|
||
public GameObject content8;
|
||
|
||
public List<string> OpcName = new List<string>(); //保存职业任务介绍
|
||
private int itemCount=1; // 要创建的物体数量
|
||
public GameObject itemPrefab; // 子物体的预制件
|
||
public JSONReader JSONReader;
|
||
#endregion
|
||
|
||
public void Start()
|
||
{
|
||
OcpName(8002, 1);
|
||
OcpName(8003, 2);
|
||
OcpName(8004, 3);
|
||
OcpName(8006, 4);
|
||
//职业介绍框左按钮
|
||
OpcLeft.onClick.AddListener(() =>
|
||
{
|
||
LeftRight(1);
|
||
});
|
||
//职业介绍框右按钮
|
||
OpcRight.onClick.AddListener(() => {
|
||
LeftRight(2);
|
||
});
|
||
}
|
||
public void Update()
|
||
{
|
||
#region 测试
|
||
if (Input.GetKeyDown("j"))
|
||
{
|
||
// PopAccident(TestImage);
|
||
}
|
||
#endregion
|
||
if (isFadeEnd)
|
||
{
|
||
StopCoroutine(FadeInOutRoutine());
|
||
isFadeEnd = false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 打开特殊情况弹窗
|
||
/// </summary>
|
||
public void PopTipPanel()
|
||
{
|
||
TipPanel.SetActive(true);
|
||
StopCoroutine(FadeInOutRoutine());
|
||
}
|
||
/// <summary>
|
||
/// 弹出事故框框并且修改图片
|
||
/// </summary>
|
||
/// <param name="image">传入一张事故图片(Spite)</param>
|
||
public void PopAccident(Sprite image)
|
||
{
|
||
AccidentImage.sprite = image;
|
||
AccidentIPanel.SetActive(true);
|
||
}
|
||
/// <summary>
|
||
/// 提示面板淡入淡出的携程
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
IEnumerator FadeInOutRoutine()
|
||
{
|
||
isFading = true; // 设置正在淡入淡出标志
|
||
|
||
//// 淡入
|
||
float elapsedTime = 0f;
|
||
while (elapsedTime < fadeInDuration)
|
||
{
|
||
elapsedTime += Time.deltaTime;
|
||
//Color textColor = TipText.color;
|
||
//textColor.a = elapsedTime / fadeInDuration; // 根据时间计算alpha值
|
||
//TipText.color = textColor;
|
||
//TipBg.color = textColor;
|
||
|
||
yield return null; // 等待下一帧
|
||
}
|
||
|
||
// 显示文本一段时间
|
||
yield return new WaitForSeconds(displayDuration);
|
||
|
||
// 淡出
|
||
elapsedTime = 0f;
|
||
while (elapsedTime < fadeOutDuration)
|
||
{
|
||
elapsedTime += Time.deltaTime;
|
||
Color textColor = TipText.color;
|
||
textColor.a = 1f - (elapsedTime / fadeOutDuration); // 根据时间计算alpha值
|
||
TipText.color = textColor;
|
||
TipBg.color = textColor;
|
||
yield return null; // 等待下一帧
|
||
}
|
||
|
||
isFading = false; // 重置正在淡入淡出标志
|
||
isFadeEnd = true;//这个携程结束的标志
|
||
TipPanel.SetActive(false);
|
||
}
|
||
/// <summary>
|
||
/// 打开关闭职业选择面板
|
||
/// </summary>
|
||
public void OpenOccupation()
|
||
{
|
||
bool isActive = OccupationPanel.activeSelf;
|
||
// 如果面板已激活,则关闭,否则打开
|
||
OccupationPanel.SetActive(!isActive);
|
||
}
|
||
/// <summary>
|
||
/// 获取职业名字并且赋值到面板上
|
||
/// </summary>
|
||
/// <param name="ID">职业的ID</param>
|
||
/// <param name="a">第几个面板(从1开始)</param>
|
||
public void OcpName(int ID, int index)
|
||
{
|
||
string str = JSONReader.GetOcpName(ID);
|
||
switch (index)
|
||
{
|
||
case 1:
|
||
CreateOcpText(ID.ToString(), content1);
|
||
Ocp1.text = str;
|
||
break;
|
||
case 2:
|
||
CreateOcpText(ID.ToString(), content2);
|
||
Ocp2.text = str;
|
||
break;
|
||
case 3:
|
||
CreateOcpText(ID.ToString(), content3);
|
||
Ocp3.text = str;
|
||
break;
|
||
case 4:
|
||
CreateOcpText(ID.ToString(), content4);
|
||
Ocp4.text = str;
|
||
break;
|
||
case 5:
|
||
CreateOcpText(ID.ToString(), content5);
|
||
Ocp5.text = str;
|
||
break;
|
||
case 6:
|
||
CreateOcpText(ID.ToString(), content6);
|
||
Ocp6.text = str;
|
||
break;
|
||
case 7:
|
||
CreateOcpText(ID.ToString(), content7);
|
||
Ocp7.text = str;
|
||
break;
|
||
case 8:
|
||
CreateOcpText(ID.ToString(), content8);
|
||
Ocp8.text = str;
|
||
break;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 创建职业介绍内容(这个方法要联动OcpName方法的)
|
||
/// </summary>
|
||
/// <param name="ID">职业的ID</param>
|
||
/// <param name="content">放在那个一个子物体下</param>
|
||
public void CreateOcpText(string ID,GameObject content)
|
||
{
|
||
OpcName.Clear();
|
||
// 遍历 SelectDictionaryctsDictionary.Count);
|
||
foreach (var kvp in JSONReader.ZZSelectsDictionary)
|
||
{
|
||
// 这里 kvp.Key 是字典的键,kvp.Value 是 Select 对象
|
||
Select select = kvp.Value;
|
||
|
||
// 判断 UIDetails 是否匹配,并且填充数据
|
||
if (select.UIDetails == ID.ToString())
|
||
{
|
||
// 将 Note 添加到 OpcName 列表
|
||
OpcName.Add(select.Note);
|
||
}
|
||
}
|
||
// 动态创建指定数量的子物体并保存它们
|
||
for (int i = 0; i < OpcName.Count; i++)
|
||
{
|
||
GameObject newItem = Instantiate(itemPrefab, content.transform);
|
||
Text left = newItem.transform.Find("Text").GetComponent<Text>();
|
||
left.text = OpcName[i];
|
||
|
||
createdItems.Add(newItem); // 将新创建的物体添加到列表中
|
||
}
|
||
}
|
||
public void LeftRight(int a)
|
||
{
|
||
if(a==1)
|
||
{
|
||
OpcTip1.SetActive(true);
|
||
OpcTip2.SetActive(true);
|
||
OpcTip3.SetActive(true);
|
||
OpcTip4.SetActive(true);
|
||
OpcTip5.SetActive(false);
|
||
OpcTip6.SetActive(false);
|
||
OpcTip7.SetActive(false);
|
||
OpcTip8.SetActive(false);
|
||
|
||
}
|
||
else
|
||
{
|
||
OpcTip1.SetActive(false);
|
||
OpcTip2.SetActive(false);
|
||
OpcTip3.SetActive(false);
|
||
OpcTip4.SetActive(false);
|
||
OpcTip5.SetActive(true);
|
||
OpcTip6.SetActive(true);
|
||
OpcTip7.SetActive(true);
|
||
OpcTip8.SetActive(true);
|
||
}
|
||
}
|
||
}
|