MoveItemManager.cs 10 KB

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