275 lines
8.1 KiB
C#
275 lines
8.1 KiB
C#
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>
|
||
private bool weakening_state;
|
||
|
||
Button left_button;
|
||
Button right_button;
|
||
|
||
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();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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));
|
||
|
||
right_button.onClick.AddListener(Right);
|
||
left_button.onClick.AddListener(Left);
|
||
|
||
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();
|
||
UpdateOrder();
|
||
}
|
||
|
||
/// <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);
|
||
}
|
||
}
|
||
UpdateText();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 左切换
|
||
/// </summary>
|
||
void Left()
|
||
{
|
||
if (current_component_scene_items.Count <= 1) return;
|
||
if (weakening_state)
|
||
{
|
||
/*
|
||
* 终结掉
|
||
* current_component_scene_items[Last].SetWeakening();
|
||
* current_component_scene_items[Next].SetWeakening();
|
||
*/
|
||
DOTween.KillAll();
|
||
}
|
||
|
||
//反向移动
|
||
Current = Last;
|
||
|
||
//scene_name_text.text = free_scene_items[Current].free_scene_item_data.scene_name;
|
||
|
||
current_component_scene_items[Last].SetAnchoredPositionLeft();
|
||
current_component_scene_items[Current].SetAnchoredPositionMiddle(true);
|
||
current_component_scene_items[Next].SetAnchoredPositionRight();
|
||
|
||
//居中其他,并隐藏
|
||
for (int i = 0; i < current_component_scene_items.Count; i++)
|
||
{
|
||
if (i != Current && i != Last && i != Next) { current_component_scene_items[i].SetAnchoredPositionMiddle(false); }
|
||
}
|
||
scene_name_clock_scrolling.ScrollingUp();
|
||
UpdateText();
|
||
|
||
timer = 0;
|
||
weakening_state = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 右切换
|
||
/// </summary>
|
||
void Right()
|
||
{
|
||
if (current_component_scene_items.Count <= 1) return;
|
||
if (weakening_state)
|
||
{
|
||
/*
|
||
* 终结掉
|
||
* current_component_scene_items[Last].SetWeakening();
|
||
* current_component_scene_items[Next].SetWeakening();
|
||
*/
|
||
DOTween.KillAll();
|
||
}
|
||
|
||
//正向移动
|
||
Current = Next;
|
||
|
||
current_component_scene_items[Last].SetAnchoredPositionLeft();
|
||
current_component_scene_items[Current].SetAnchoredPositionMiddle(true);
|
||
current_component_scene_items[Next].SetAnchoredPositionRight();
|
||
|
||
//居中其他,并隐藏
|
||
for (int i = 0; i < current_component_scene_items.Count; i++)
|
||
{
|
||
if (i != Current && i != Last && i != Next) { current_component_scene_items[i].SetAnchoredPositionMiddle(false); }
|
||
}
|
||
scene_name_clock_scrolling.ScrollingDown();
|
||
UpdateText();
|
||
|
||
timer = 0;
|
||
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);
|
||
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();
|
||
}
|
||
}
|
||
|
||
protected override void OnDisable()
|
||
{
|
||
base.OnDisable();
|
||
current_component_scene_items.ForEach(item => item.gameObject.SetActive(false));
|
||
current_component_scene_items.Clear();
|
||
}
|
||
}
|