MoveItemManager.cs 9.9 KB

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