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 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(new PrinciplePanelData() { TitleName = deviceName}); } /// /// 正向播放 /// /// public void PlayMoveItemByIndex(int index = 0) { StopAllCoroutines(); Debug.Log("播放" + moveItemInfos[index].moveItem.name); UIKit.OpenPanel(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); } })); } /// /// 正向播放完成一条回调 /// /// 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(); if (m_Reverse) { PlayMoveItemByReverseIndex(moveItemInfos.Count - 1); } } else { PlayMoveItemByIndex(index + 1); } } /// /// 使用音频控制结束 /// bool useAudioCtrl; /// /// 反向播放 /// /// public void PlayMoveItemByReverseIndex(int index) { StopAllCoroutines(); Debug.Log("反向播放" + moveItemInfos[index].moveItem.name); UIKit.OpenPanel(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); } })); } /// /// 反向播放完成一条回调 /// /// public void OnPlayMoveItemByReverseIndexCallBack(int index) { moveItemInfos[index].finish = false; if (index - 1 < 0) { Debug.Log("反向播放序列完成"); UIKit.ClosePanel(); } 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(); } /// /// 获取所有的组件 /// [TabGroup("配置相关")] [Button("获取所有的组件")] [GUIColor("GetColor")] private void GetAllMoveItems() { moveItemInfos = new List(); MoveItem[] moveItems = transform.GetComponentsInChildren(); 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; } } /// /// 播放按钮点击 /// private void OnPlayButtonClick() { if (Time.timeScale == 0) { Time.timeScale = 1; if (m_PlayButton) { m_PlayButton.transform.Find("Text").GetComponent().text = "暂停"; } AudioHelper.AudioPlay(); } else { Time.timeScale = 0; if (m_PlayButton) { m_PlayButton.transform.Find("Text").GetComponent().text = "播放"; } AudioHelper.AudioPause(); } } /// /// 导出部件数据 /// [TabGroup("工具相关")] [Button("1.导出所有部件名称到表格(先去根据表格配音)")] private void ExportPartData() { List partMarkNames = new List(); 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(Application.streamingAssetsPath + "/" + deviceName + GlobalConfig.excelSuffix, partMarkNames); } /// /// 重命名音频名称,只能重命名数字的文件 /// [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 tmpMoveDatas = ExcelHelper.ReadInfoFromExcel(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("父级对象")] /// /// 初始化位置 /// [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; }