123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using System;
- using UnityEngine.UI;
- using System.IO;
- using System.Linq;
- using QFramework;
- public class MoveItemManager : MonoBehaviour
- {
- [TabGroup("配置相关")]
- [LabelText("设备名称")]
- [ValidateInput("IsValid")]
- public string deviceName;
- private bool IsValid(string value)
- {
- return !string.IsNullOrEmpty(value);
- }
- [TabGroup("配置相关")]
- [TableList]
- public List<MoveItemInfo> moveItemInfos;
-
- [TabGroup("配置相关")]
- [LabelText("播完反向播放")]
- public bool m_Reverse;
- [TabGroup("UI相关")]
- [LabelText("播放按钮")]
- public Button m_PlayButton;
- private void Awake()
- {
- ResKit.Init();
- }
- private void Start()
- {
- m_PlayButton?.onClick.AddListener(OnPlayButtonClick);
- PlayMoveItemByIndex(0);
- UIKit.OpenPanel<PrinciplePanel>(new PrinciplePanelData() { TitleName = deviceName});
- }
- /// <summary>
- /// 正向播放
- /// </summary>
- /// <param name="index"></param>
- public void PlayMoveItemByIndex(int index = 0)
- {
- StopAllCoroutines();
- Debug.Log("播放" + moveItemInfos[index].moveItem.name);
- UIKit.OpenPanel<ShowPartNamePanel>(new ShowPartNamePanelData() {partName = "拆下" + moveItemInfos[index].moveItem.name });
- StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, "拆下" + moveItemInfos[index].moveItem.name,
- len =>
- {
- if (len > moveItemInfos[index].moveItem.m_Duration)
- {
- Debug.Log("音频长");
- useAudioCtrl = true;
- }else
- {
- Debug.Log("动画长");
- useAudioCtrl = false;
- }
- },
- () =>
- {
- if (useAudioCtrl)
- {
- Debug.Log("音频回调");
- OnPlayMoveItemByIndexCallBack(index);
- }
- }));
- StartCoroutine(MovePathByItem(moveItemInfos[index], false,
- () =>
- {
- if (!useAudioCtrl)
- {
- Debug.Log("动画回调");
- OnPlayMoveItemByIndexCallBack(index);
- }
- }));
- }
- /// <summary>
- /// 正向播放完成一条回调
- /// </summary>
- /// <param name="index"></param>
- public void OnPlayMoveItemByIndexCallBack(int index)
- {
- moveItemInfos[index].moveItem.gameObject.SetActive(false);
- moveItemInfos[index].finish = true;
- if (index + 1 > moveItemInfos.Count - 1)
- {
- Debug.Log("正播播放序列完成");
- UIKit.ClosePanel<ShowPartNamePanel>();
- if (m_Reverse)
- {
- PlayMoveItemByReverseIndex(moveItemInfos.Count - 1);
- }
- }
- else
- {
- PlayMoveItemByIndex(index + 1);
- }
- }
- /// <summary>
- /// 使用音频控制结束
- /// </summary>
- bool useAudioCtrl;
- /// <summary>
- /// 反向播放
- /// </summary>
- /// <param name="index"></param>
- public void PlayMoveItemByReverseIndex(int index)
- {
- StopAllCoroutines();
- Debug.Log("反向播放" + moveItemInfos[index].moveItem.name);
- UIKit.OpenPanel<ShowPartNamePanel>(new ShowPartNamePanelData() { partName = "安装" + moveItemInfos[index].moveItem.name });
- StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, "安装" + moveItemInfos[index].moveItem.name,len =>
- {
- if (len > moveItemInfos[index].moveItem.m_Duration)
- {
- Debug.Log("音频长");
- useAudioCtrl = true;
- }
- else
- {
- Debug.Log("动画长");
- useAudioCtrl = false;
- }
- },
- () =>
- {
- if (useAudioCtrl)
- {
- Debug.Log("音频回调");
- OnPlayMoveItemByReverseIndexCallBack(index);
- }
- }));
- StartCoroutine(MovePathByItem(moveItemInfos[index], true,
- () =>
- {
- if (!useAudioCtrl)
- {
- Debug.Log("动画回调");
- OnPlayMoveItemByReverseIndexCallBack(index);
- }
- }));
- }
- /// <summary>
- /// 反向播放完成一条回调
- /// </summary>
- /// <param name="index"></param>
- public void OnPlayMoveItemByReverseIndexCallBack(int index)
- {
- moveItemInfos[index].finish = false;
- if (index - 1 < 0)
- {
- Debug.Log("反向播放序列完成");
- UIKit.ClosePanel<ShowPartNamePanel>();
- }
- else
- {
- PlayMoveItemByReverseIndex(index - 1);
- }
- }
- IEnumerator MovePathByItem(MoveItemInfo moveItemInfo, bool isReverse, Action finishedCallBack = null)
- {
- float timer = 0;
- float lerp = 0;
- while (timer < moveItemInfo.moveItem.m_Duration)
- {
- MoveItem moveItem = moveItemInfo.moveItem;
- moveItem.gameObject.SetActive(true);
- moveItem.OpenHighter(true);
- lerp = isReverse? (1 - timer / moveItem.m_Duration) :timer / moveItem.m_Duration;
- if (moveItem.m_UserGlobalDirection)
- {
- moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
- moveItemInfo.initPosition + moveItem.m_Direction, lerp);
- }
- else
- {
- moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
- moveItemInfo.initPosition + moveItem.transform.TransformDirection(moveItem.m_Direction), lerp);
- }
- timer += Time.deltaTime;
- yield return new WaitForEndOfFrame();
- }
- moveItemInfo.moveItem?.OpenHighter(false);
- finishedCallBack.Invoke();
- }
- /// <summary>
- /// 获取所有的组件
- /// </summary>
- [TabGroup("配置相关")]
- [Button("获取所有的组件")]
- [GUIColor("GetColor")]
- private void GetAllMoveItems()
- {
- moveItemInfos = new List<MoveItemInfo>();
- MoveItem[] moveItems = transform.GetComponentsInChildren<MoveItem>();
- foreach (var item in moveItems)
- {
- MoveItemInfo moveItemInfo = new MoveItemInfo();
- moveItemInfo.moveItem = item;
- moveItemInfo.initPosition = item.transform.position;
- moveItemInfos.Add(moveItemInfo);
- }
- }
-
- private Color GetColor()
- {
- if (moveItemInfos.Count == 0 || moveItemInfos == null)
- {
- return Color.red;
- }else
- {
- return Color.green;
- }
- }
- /// <summary>
- /// 播放按钮点击
- /// </summary>
- private void OnPlayButtonClick()
- {
- if (Time.timeScale == 0)
- {
- Time.timeScale = 1;
- if (m_PlayButton)
- {
- m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "暂停";
- }
- AudioHelper.AudioPlay();
- }
- else
- {
- Time.timeScale = 0;
- if (m_PlayButton)
- {
- m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "播放";
- }
- AudioHelper.AudioPause();
- }
- }
- /// <summary>
- /// 导出部件数据
- /// </summary>
- [TabGroup("工具相关")]
- [Button("1.导出所有部件名称到表格(先去根据表格配音)")]
- private void ExportPartData()
- {
- List<MoveData> partMarkNames = new List<MoveData>();
- foreach (var item in moveItemInfos)
- {
- MoveData partMarkName = new MoveData();
- partMarkName.moveName = "拆下" + item.moveItem.name;
- if (partMarkNames.Find(t => t.moveName == partMarkName.moveName) == null) partMarkNames.Add(partMarkName);
- }
- foreach (var item in moveItemInfos)
- {
- MoveData partMarkName = new MoveData();
- partMarkName.moveName = "安装" + item.moveItem.name;
- if (partMarkNames.Find(t => t.moveName == partMarkName.moveName) == null) partMarkNames.Add(partMarkName);
- }
- ExcelHelper.WriteInfoToExcel<MoveData>(Application.streamingAssetsPath + "/" + deviceName + GlobalConfig.excelSuffix, partMarkNames);
- }
- /// <summary>
- /// 重命名音频名称,只能重命名数字的文件
- /// </summary>
- [TabGroup("工具相关")]
- [Button("2.重命名音频名称(初始配音名字为数字)")]
- private void ReNameAudioClipName()
- {
- //表格路径
- string tmpExcelPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath,deviceName) + GlobalConfig.excelSuffix;
- //音频路径完整路径信息
- string audioPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, "Config/Audios/内部结构拆解/" + deviceName);
- List<MoveData> tmpMoveDatas = ExcelHelper.ReadInfoFromExcel<MoveData>(tmpExcelPath,1);
- DirectoryInfo directoryInfo = new DirectoryInfo(audioPath);
- FileInfo[] files = directoryInfo.GetFiles("*.mp3");
- FileInfo[] sortedFiles = files.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f.FullName))).ToArray();
- if (tmpMoveDatas.Count != sortedFiles.Length)
- {
- Debug.LogError("重命名失败");
- }
- else
- {
- for (int i = 0; i < tmpMoveDatas.Count; i++)
- {
- string newPath = Path.Combine(directoryInfo.FullName, tmpMoveDatas[i].moveName) + ".mp3";
- sortedFiles[i].MoveTo(newPath);
- }
- }
- }
- }
- [Serializable]
- public class MoveItemInfo
- {
- [GUIColor("FinishedStateColor")]
- public MoveItem moveItem;
- [LabelText("父级对象")]
- /// <summary>
- /// 初始化位置
- /// </summary>
- [HideInInspector]
- public Vector3 initPosition;
- [LabelText("是否完成")]
- public bool finish;
- private Color FinishedStateColor()
- {
- if (finish)
- {
- return new Color(0.7f, 1f, 0.7f);
- }
- return GUI.color;
- }
- }
- public class MoveData
- {
- public string moveName;
- }
|