MoveItemManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using System;
  6. using UnityEngine.UI;
  7. using System.IO;
  8. using System.Linq;
  9. using QFramework;
  10. public class MoveItemManager : MonoBehaviour
  11. {
  12. [TabGroup("配置相关")]
  13. [LabelText("设备名称")]
  14. [ValidateInput("IsValid")]
  15. public string deviceName;
  16. private bool IsValid(string value)
  17. {
  18. return !string.IsNullOrEmpty(value);
  19. }
  20. [TabGroup("配置相关")]
  21. [TableList]
  22. public List<MoveItemInfo> moveItemInfos;
  23. [TabGroup("配置相关")]
  24. [LabelText("播完反向播放")]
  25. public bool m_Reverse;
  26. [TabGroup("UI相关")]
  27. [LabelText("播放按钮")]
  28. public Button m_PlayButton;
  29. private void Awake()
  30. {
  31. ResKit.Init();
  32. }
  33. private void Start()
  34. {
  35. m_PlayButton?.onClick.AddListener(OnPlayButtonClick);
  36. PlayMoveItemByIndex(0);
  37. UIKit.OpenPanel<PrinciplePanel>(new PrinciplePanelData() { TitleName = deviceName});
  38. }
  39. /// <summary>
  40. /// 正向播放
  41. /// </summary>
  42. /// <param name="index"></param>
  43. public void PlayMoveItemByIndex(int index = 0)
  44. {
  45. StopAllCoroutines();
  46. Debug.Log("播放" + moveItemInfos[index].moveItem.name);
  47. UIKit.OpenPanel<ShowPartNamePanel>(new ShowPartNamePanelData() {partName = "拆下" + moveItemInfos[index].moveItem.name });
  48. StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, "拆下" + moveItemInfos[index].moveItem.name,
  49. len =>
  50. {
  51. if (len > moveItemInfos[index].moveItem.m_Duration)
  52. {
  53. Debug.Log("音频长");
  54. useAudioCtrl = true;
  55. }else
  56. {
  57. Debug.Log("动画长");
  58. useAudioCtrl = false;
  59. }
  60. },
  61. () =>
  62. {
  63. if (useAudioCtrl)
  64. {
  65. Debug.Log("音频回调");
  66. OnPlayMoveItemByIndexCallBack(index);
  67. }
  68. }));
  69. StartCoroutine(MovePathByItem(moveItemInfos[index], false,
  70. () =>
  71. {
  72. if (!useAudioCtrl)
  73. {
  74. Debug.Log("动画回调");
  75. OnPlayMoveItemByIndexCallBack(index);
  76. }
  77. }));
  78. }
  79. /// <summary>
  80. /// 正向播放完成一条回调
  81. /// </summary>
  82. /// <param name="index"></param>
  83. public void OnPlayMoveItemByIndexCallBack(int index)
  84. {
  85. moveItemInfos[index].moveItem.gameObject.SetActive(false);
  86. moveItemInfos[index].finish = true;
  87. if (index + 1 > moveItemInfos.Count - 1)
  88. {
  89. Debug.Log("正播播放序列完成");
  90. UIKit.ClosePanel<ShowPartNamePanel>();
  91. if (m_Reverse)
  92. {
  93. PlayMoveItemByReverseIndex(moveItemInfos.Count - 1);
  94. }
  95. }
  96. else
  97. {
  98. PlayMoveItemByIndex(index + 1);
  99. }
  100. }
  101. /// <summary>
  102. /// 使用音频控制结束
  103. /// </summary>
  104. bool useAudioCtrl;
  105. /// <summary>
  106. /// 反向播放
  107. /// </summary>
  108. /// <param name="index"></param>
  109. public void PlayMoveItemByReverseIndex(int index)
  110. {
  111. StopAllCoroutines();
  112. Debug.Log("反向播放" + moveItemInfos[index].moveItem.name);
  113. UIKit.OpenPanel<ShowPartNamePanel>(new ShowPartNamePanelData() { partName = "安装" + moveItemInfos[index].moveItem.name });
  114. StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, "安装" + moveItemInfos[index].moveItem.name,len =>
  115. {
  116. if (len > moveItemInfos[index].moveItem.m_Duration)
  117. {
  118. Debug.Log("音频长");
  119. useAudioCtrl = true;
  120. }
  121. else
  122. {
  123. Debug.Log("动画长");
  124. useAudioCtrl = false;
  125. }
  126. },
  127. () =>
  128. {
  129. if (useAudioCtrl)
  130. {
  131. Debug.Log("音频回调");
  132. OnPlayMoveItemByReverseIndexCallBack(index);
  133. }
  134. }));
  135. StartCoroutine(MovePathByItem(moveItemInfos[index], true,
  136. () =>
  137. {
  138. if (!useAudioCtrl)
  139. {
  140. Debug.Log("动画回调");
  141. OnPlayMoveItemByReverseIndexCallBack(index);
  142. }
  143. }));
  144. }
  145. /// <summary>
  146. /// 反向播放完成一条回调
  147. /// </summary>
  148. /// <param name="index"></param>
  149. public void OnPlayMoveItemByReverseIndexCallBack(int index)
  150. {
  151. moveItemInfos[index].finish = false;
  152. if (index - 1 < 0)
  153. {
  154. Debug.Log("反向播放序列完成");
  155. UIKit.ClosePanel<ShowPartNamePanel>();
  156. }
  157. else
  158. {
  159. PlayMoveItemByReverseIndex(index - 1);
  160. }
  161. }
  162. IEnumerator MovePathByItem(MoveItemInfo moveItemInfo, bool isReverse, Action finishedCallBack = null)
  163. {
  164. float timer = 0;
  165. float lerp = 0;
  166. while (timer < moveItemInfo.moveItem.m_Duration)
  167. {
  168. MoveItem moveItem = moveItemInfo.moveItem;
  169. moveItem.gameObject.SetActive(true);
  170. moveItem.OpenHighter(true);
  171. lerp = isReverse? (1 - timer / moveItem.m_Duration) :timer / moveItem.m_Duration;
  172. if (moveItem.m_UserGlobalDirection)
  173. {
  174. moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
  175. moveItemInfo.initPosition + moveItem.m_Direction, lerp);
  176. }
  177. else
  178. {
  179. moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
  180. moveItemInfo.initPosition + moveItem.transform.TransformDirection(moveItem.m_Direction), lerp);
  181. }
  182. timer += Time.deltaTime;
  183. yield return new WaitForEndOfFrame();
  184. }
  185. moveItemInfo.moveItem?.OpenHighter(false);
  186. finishedCallBack.Invoke();
  187. }
  188. /// <summary>
  189. /// 获取所有的组件
  190. /// </summary>
  191. [TabGroup("配置相关")]
  192. [Button("获取所有的组件")]
  193. [GUIColor("GetColor")]
  194. private void GetAllMoveItems()
  195. {
  196. moveItemInfos = new List<MoveItemInfo>();
  197. MoveItem[] moveItems = transform.GetComponentsInChildren<MoveItem>();
  198. foreach (var item in moveItems)
  199. {
  200. MoveItemInfo moveItemInfo = new MoveItemInfo();
  201. moveItemInfo.moveItem = item;
  202. moveItemInfo.initPosition = item.transform.position;
  203. moveItemInfos.Add(moveItemInfo);
  204. }
  205. }
  206. private Color GetColor()
  207. {
  208. if (moveItemInfos.Count == 0 || moveItemInfos == null)
  209. {
  210. return Color.red;
  211. }else
  212. {
  213. return Color.green;
  214. }
  215. }
  216. /// <summary>
  217. /// 播放按钮点击
  218. /// </summary>
  219. private void OnPlayButtonClick()
  220. {
  221. if (Time.timeScale == 0)
  222. {
  223. Time.timeScale = 1;
  224. if (m_PlayButton)
  225. {
  226. m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "暂停";
  227. }
  228. AudioHelper.AudioPlay();
  229. }
  230. else
  231. {
  232. Time.timeScale = 0;
  233. if (m_PlayButton)
  234. {
  235. m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "播放";
  236. }
  237. AudioHelper.AudioPause();
  238. }
  239. }
  240. /// <summary>
  241. /// 导出部件数据
  242. /// </summary>
  243. [TabGroup("工具相关")]
  244. [Button("1.导出所有部件名称到表格(先去根据表格配音)")]
  245. private void ExportPartData()
  246. {
  247. List<MoveData> partMarkNames = new List<MoveData>();
  248. foreach (var item in moveItemInfos)
  249. {
  250. MoveData partMarkName = new MoveData();
  251. partMarkName.moveName = "拆下" + item.moveItem.name;
  252. if (partMarkNames.Find(t => t.moveName == partMarkName.moveName) == null) partMarkNames.Add(partMarkName);
  253. }
  254. foreach (var item in moveItemInfos)
  255. {
  256. MoveData partMarkName = new MoveData();
  257. partMarkName.moveName = "安装" + item.moveItem.name;
  258. if (partMarkNames.Find(t => t.moveName == partMarkName.moveName) == null) partMarkNames.Add(partMarkName);
  259. }
  260. ExcelHelper.WriteInfoToExcel<MoveData>(Application.streamingAssetsPath + "/" + deviceName + GlobalConfig.excelSuffix, partMarkNames);
  261. }
  262. /// <summary>
  263. /// 重命名音频名称,只能重命名数字的文件
  264. /// </summary>
  265. [TabGroup("工具相关")]
  266. [Button("2.重命名音频名称(初始配音名字为数字)")]
  267. private void ReNameAudioClipName()
  268. {
  269. //表格路径
  270. string tmpExcelPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath,deviceName) + GlobalConfig.excelSuffix;
  271. //音频路径完整路径信息
  272. string audioPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, "Config/Audios/内部结构拆解/" + deviceName);
  273. List<MoveData> tmpMoveDatas = ExcelHelper.ReadInfoFromExcel<MoveData>(tmpExcelPath,1);
  274. DirectoryInfo directoryInfo = new DirectoryInfo(audioPath);
  275. FileInfo[] files = directoryInfo.GetFiles("*.mp3");
  276. FileInfo[] sortedFiles = files.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f.FullName))).ToArray();
  277. if (tmpMoveDatas.Count != sortedFiles.Length)
  278. {
  279. Debug.LogError("重命名失败");
  280. }
  281. else
  282. {
  283. for (int i = 0; i < tmpMoveDatas.Count; i++)
  284. {
  285. string newPath = Path.Combine(directoryInfo.FullName, tmpMoveDatas[i].moveName) + ".mp3";
  286. sortedFiles[i].MoveTo(newPath);
  287. }
  288. }
  289. }
  290. }
  291. [Serializable]
  292. public class MoveItemInfo
  293. {
  294. [GUIColor("FinishedStateColor")]
  295. public MoveItem moveItem;
  296. [LabelText("父级对象")]
  297. /// <summary>
  298. /// 初始化位置
  299. /// </summary>
  300. [HideInInspector]
  301. public Vector3 initPosition;
  302. [LabelText("是否完成")]
  303. public bool finish;
  304. private Color FinishedStateColor()
  305. {
  306. if (finish)
  307. {
  308. return new Color(0.7f, 1f, 0.7f);
  309. }
  310. return GUI.color;
  311. }
  312. }
  313. public class MoveData
  314. {
  315. public string moveName;
  316. }