ict.shenzhi/Assets/Scripts/UIManager/Panel/FreeProgrammingPanel.cs

318 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;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.SceneManagement;
using static InterfaceManager;
using static LoginSceneUIManager;
using System.IO;
using Newtonsoft.Json;
using TMPro;
using System.Linq;
/// <summary>
/// 自由编程面板
/// </summary>
public class FreeProgrammingPanel : PanelBasic
{
/// <summary>
/// 进入场景按钮
/// </summary>
public Button enter_scene_button;
/// <summary>
/// 当前场景名称
/// </summary>
public TextMeshProUGUI scene_name_text;
/// <summary>
/// 实例化节点
/// </summary>
public Transform instantiate_root;
/// <summary>
/// 预制体
/// </summary>
public FreeSceneItem free_scene_item_prefab;
/// <summary>
/// 实例化的所有场景对象
/// </summary>
public List<FreeSceneItem> free_scene_items = new List<FreeSceneItem>();
/// <summary>
/// 当前组件的场景对象
/// </summary>
public List<FreeSceneItem> current_component_scene_items = new List<FreeSceneItem>();
/// <summary>
/// 场景名称
/// </summary>
public ClockScrolling scene_name_clock_scrolling;
/// <summary>
/// 计时器
/// </summary>
// private float timer;
/// <summary>
/// 虚化效果
/// </summary>
public bool weakening_state;
/// <summary>
/// 显示图片
/// </summary>
public Sprite[] Picture;
protected override void Awake()
{
base.Awake();
}
// Start is called before the first frame update
void Start()
{
Init();
}
private void Update()
{
// timer += Time.deltaTime;
// if (timer >= 3f)
// {
if (!weakening_state)
{
weakening_state = true;
if (Current != Next && Current != Last)
{
// current_component_scene_items[Last].SetWeakening();
// current_component_scene_items[Next].SetWeakening();
foreach (FreeSceneItem n in current_component_scene_items)
{
if (current_component_scene_items[Next] != n)
{
n.();
}
else
{
n.();
}
n.GetComponent<Image>().enabled = false;
}
// current_component_scene_items[Last].显示变大图标();
// current_component_scene_items[Next].显示变大图标();
}
}
// }
}
int Current = 0;
int Last => Current == 0 ? current_component_scene_items.Count - 1 : Current - 1;
int Next => Current == current_component_scene_items.Count - 1 ? 0 : Current + 1;
void Init()
{
free_scene_item_prefab ??= Resources.Load<FreeSceneItem>("Prefabs/UIItem/FreeSceneItem");
//读取场景配置文件
StartCoroutine(Get(Path.Combine(Application.streamingAssetsPath, "LocalConfig/FreeScene.txt"), false, OnFreeDataUpdated));
enter_scene_button.onClick.AddListener(() =>
{
if (SceneManager.GetSceneByName(current_preselect_scene) != null)
{
GameManager.current_main_menu_type = MainMenuType.;
loading_panel.OnActive(this);
}
});
}
void OnFreeDataUpdated(string _data)
{
var _free_scene_item_datas = JsonConvert.DeserializeObject<FreeSceneItem.SceneItemDetailData[]>(_data);
for (int i = 0; i < _free_scene_item_datas.Length; i++)
{
var _free_scene_item = Instantiate(free_scene_item_prefab, instantiate_root);
_free_scene_item.Init(_free_scene_item_datas[i]);
_free_scene_item.name = i.ToString();
free_scene_items.Add(_free_scene_item);
if (_free_scene_item.free_scene_item_data.scene_type == GameManager.current_component_type.ToString())
{
current_component_scene_items.Add(_free_scene_item);
}
else
_free_scene_item.SetActiveManual(false);
}
GameManager.scene_detail_datas = _free_scene_item_datas.ToList();
//自己添加的场景按钮功能
Debug.Log(current_component_scene_items.Count);
free_scene_items[0].transform.GetChild(1).GetComponent<Button>().onClick.AddListener(1);
free_scene_items[0].transform.GetChild(5).GetComponent<Button>().onClick.AddListener(1);
free_scene_items[1].transform.GetChild(1).GetComponent<Button>().onClick.AddListener(2);
free_scene_items[1].transform.GetChild(5).GetComponent<Button>().onClick.AddListener(2);
//自己写的添加图片的部分
foreach (FreeSceneItem M in free_scene_items)
{
switch (M.free_scene_item_data.scene_name)
{
case "深圳市民中心":
M.image_icon.sprite = Picture[0];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[1];
break;
case "西丽湖校区":
M.image_icon.sprite = Picture[2];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[3];
M.transform.GetChild(5).GetComponent<Image>().enabled = false;
break;
case "机器人舞台":
M.image_icon.sprite = Picture[4];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[5];
break;
case "数字人舞台":
M.image_icon.sprite = Picture[6];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[7];
break;
}
}
UpdateOrder();
/*
//自己添加的场景按钮功能
Debug.Log(current_component_scene_items.Count);
if (current_component_scene_items.Count > 1)
{
current_component_scene_items[0].transform.GetChild(1).GetComponent<Button>().onClick.AddListener(场景1);
current_component_scene_items[0].transform.GetChild(5).GetComponent<Button>().onClick.AddListener(场景1);
current_component_scene_items[1].transform.GetChild(1).GetComponent<Button>().onClick.AddListener(场景2);
current_component_scene_items[1].transform.GetChild(5).GetComponent<Button>().onClick.AddListener(场景2);
}
//自己写的添加图片的部分
foreach (FreeSceneItem M in free_scene_items)
{
switch (M.free_scene_item_data.scene_name)
{
case "深圳市民中心":
M.image_icon.sprite = Picture[0];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[1];
break;
case "西丽湖校区":
M.image_icon.sprite = Picture[2];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[3];
break;
case "机器人舞台":
M.image_icon.sprite = Picture[4];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[5];
break;
case "数字人舞台":
M.image_icon.sprite = Picture[6];
M.transform.GetChild(5).GetComponent<Image>().sprite = Picture[7];
break;
}
}
*/
UpdateText();
}
/// <summary>
/// 更新顺序及文字
/// </summary>
void UpdateOrder()
{
for (int i = 0; i < current_component_scene_items.Count; i++)
{
var _item = current_component_scene_items[i];
//第Length-1第0个第1个
if (i == Current)
{
//当前的放在最后一个位置
_item.SetAnchoredPositionMiddle(true);
}
else if (i == Next)
{
//下一个放在倒数第二个位置,右边
_item.SetAnchoredPositionRight();
}
else if (i == Last)
{
//上一个放在倒数第三个位置,左边
_item.SetAnchoredPositionLeft();
}
else
{
//其余的默认隐藏,中间,以方便切换
_item.SetAnchoredPositionMiddle(false);
}
}
}
void 1()
{
Debug.Log("切换场景1");
if (current_component_scene_items.Count <= 1) return;
Current = 0;
UpdateText();
Debug.Log(current_component_scene_items[Next].name);
weakening_state = false;
}
void 2()
{
Debug.Log("切换场景2");
if (current_component_scene_items.Count <= 1) return;
Current = 1;
UpdateText();
Debug.Log(current_component_scene_items[Next].name);
weakening_state = false;
}
/// <summary>
/// 更新场景名称显示
/// </summary>
private void UpdateText()
{
current_preselect_scene = scene_name_clock_scrolling.SetCurrentText(current_component_scene_items[Current].free_scene_item_data.scene_name);
Debug.Log("当前加载的场景为" + current_preselect_scene);
scene_name_clock_scrolling.SetNextText(current_component_scene_items[Next].free_scene_item_data.scene_name);
scene_name_clock_scrolling.SetLastText(current_component_scene_items[Last].free_scene_item_data.scene_name);
}
protected override void OnEnable()
{
base.OnEnable();
//显示时,根据选择的组件显示对应的场景列表
Current = 0;
if (free_scene_items.Count > 0)
{
for (int i = 0; i < free_scene_items.Count; i++)
{
if (free_scene_items[i].free_scene_item_data.scene_type == GameManager.current_component_type.ToString())
{
free_scene_items[i].gameObject.SetActive(true);
current_component_scene_items.Add(free_scene_items[i]);
}
}
}
if (current_component_scene_items.Count > 0)
{
UpdateOrder();
}
if (current_component_scene_items.Count != 0)
{
UpdateText();
}
}
protected override void OnDisable()
{
base.OnDisable();
current_component_scene_items.ForEach(item => item.gameObject.SetActive(false));
current_component_scene_items.Clear();
}
}