MoveItemManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. using System.Reflection;
  11. public class MoveItemManager : MonoBehaviour
  12. {
  13. public static MoveItemManager instance;
  14. [TabGroup("配置相关")]
  15. [LabelText("设备名称")]
  16. [ValidateInput("IsValid")]
  17. public string deviceName;
  18. private bool IsValid(string value)
  19. {
  20. return !string.IsNullOrEmpty(value);
  21. }
  22. [TabGroup("配置相关")]
  23. [TableList]
  24. public List<MoveItemInfo> moveItemInfos;
  25. /// <summary>
  26. /// 使用音频控制结束
  27. /// </summary>
  28. bool useAudioCtrl;
  29. public ModelItem m_CurrentSelectModelItem;
  30. private void Awake()
  31. {
  32. instance = this;
  33. UIKit.OpenPanel<AutoDisassemblyPanel>();
  34. }
  35. private void Start()
  36. {
  37. ApplyOrderFromExcel();
  38. }
  39. /// <summary>
  40. ///根据Excel顺序重排 moveItemInfos
  41. /// </summary>
  42. public void ApplyOrderFromExcel(string devName = null)
  43. {
  44. string dn = string.IsNullOrEmpty(devName) ? deviceName : devName;
  45. List<string> excelNames = ExcelHandler.ReadFirstColumn(dn);
  46. if (excelNames == null || excelNames.Count == 0) return;
  47. Dictionary<string, MoveItemInfo> map = new Dictionary<string, MoveItemInfo>();
  48. foreach (var info in moveItemInfos)
  49. if (info != null && info.moveItem != null && !map.ContainsKey(info.moveItem.name))
  50. map.Add(info.moveItem.name, info);
  51. List<MoveItemInfo> newList = new List<MoveItemInfo>();
  52. foreach (var n in excelNames) if (map.TryGetValue(n, out var mi)) newList.Add(mi);
  53. foreach (var left in moveItemInfos) if (!excelNames.Contains(left.moveItem.name)) newList.Add(left);
  54. // 关键:清空+添加,不替换引用
  55. moveItemInfos.Clear();
  56. moveItemInfos.AddRange(newList);
  57. Debug.Log("moveItemInfos 已按 Excel 顺序重排");
  58. }
  59. /// 进入编辑时调用——停止一切并回到初始位
  60. public void HaltAndResetForEdit()
  61. {
  62. StopAllCoroutines();
  63. AudioHelper.AudioPause();
  64. Time.timeScale = 0;
  65. SetInitState(); // 你已有的方法:全部回初始位置、显示/隐藏
  66. Debug.Log("已停并归零(进入编辑模式)");
  67. }
  68. //编辑返回后调用——按表重排并回初始位(不立即播放)
  69. public void ReapplyOrderAndReset(string devName = null)
  70. {
  71. ApplyOrderFromExcel(devName);
  72. StopAllCoroutines();
  73. AudioHelper.AudioPause();
  74. Time.timeScale = 0;
  75. SetInitState();
  76. Debug.Log("已按最新表重排并归零(等待用户重新播放)");
  77. }
  78. public void Update()
  79. {
  80. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  81. RaycastHit hit;
  82. if (Physics.Raycast(ray, out hit))
  83. {
  84. ModelItem modelItem = GetParentPartMarkWithModelItem(hit.collider.transform);
  85. if (m_CurrentSelectModelItem != modelItem)
  86. {
  87. if (m_CurrentSelectModelItem != null)
  88. {
  89. m_CurrentSelectModelItem.CloseHighlighter();
  90. }
  91. m_CurrentSelectModelItem = modelItem;
  92. m_CurrentSelectModelItem.OpenHighlighter();
  93. }
  94. // 处理射线击中的对象
  95. if (modelItem != null)
  96. {
  97. UIKit.GetPanel<AutoDisassemblyPanel>().SetSubTitleText(modelItem.name);
  98. if(Input.GetMouseButtonDown(0)&& Time.timeScale == 0)
  99. {
  100. CameraSurround.instance.SetCameraPosition(modelItem.transform, false, false);
  101. }
  102. }
  103. }
  104. else
  105. {
  106. if (m_CurrentSelectModelItem != null)
  107. {
  108. m_CurrentSelectModelItem.CloseHighlighter();
  109. m_CurrentSelectModelItem = null;
  110. UIKit.GetPanel<AutoDisassemblyPanel>().SetSubTitleText("");
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 查找携带PartMark的节点
  116. /// </summary>
  117. /// <param name="traget"></param>
  118. /// <returns></returns>
  119. private ModelItem GetParentPartMarkWithModelItem(Transform traget)
  120. {
  121. ModelItem tmpMark = traget.GetComponent<ModelItem>();
  122. if (tmpMark == null && traget.parent != null)
  123. {
  124. tmpMark = GetParentPartMarkWithModelItem(traget.parent);
  125. }
  126. return tmpMark;
  127. }
  128. /// <summary>
  129. /// 正向播放
  130. /// </summary>
  131. public void PlayPlayMoveItem()
  132. {
  133. StopAllCoroutines();
  134. SetInitState();
  135. PlayMoveItemByIndex();
  136. }
  137. /// <summary>
  138. /// 反向播放
  139. /// </summary>
  140. public void PlayMoveItemByReverse()
  141. {
  142. StopAllCoroutines();
  143. SetFinalState();
  144. PlayMoveItemByReverseIndex(moveItemInfos.Count - 1);
  145. }
  146. /// <summary>
  147. /// 正向播放
  148. /// </summary>
  149. /// <param name="index"></param>
  150. private void PlayMoveItemByIndex(int index = 0)
  151. {
  152. StopAllCoroutines();
  153. Debug.Log("播放" + moveItemInfos[index].moveItem.name);
  154. UIKit.GetPanel<AutoDisassemblyPanel>().SetSubTitleText(moveItemInfos[index].moveItem.name);
  155. StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, moveItemInfos[index].moveItem.name,
  156. len =>
  157. {
  158. if (len > moveItemInfos[index].moveItem.m_Duration)
  159. {
  160. Debug.Log("音频长");
  161. useAudioCtrl = true;
  162. }
  163. else
  164. {
  165. Debug.Log("动画长");
  166. useAudioCtrl = false;
  167. }
  168. },
  169. () =>
  170. {
  171. if (useAudioCtrl)
  172. {
  173. Debug.Log("音频回调");
  174. OnPlayMoveItemByIndexCallBack(index);
  175. }
  176. }));
  177. StartCoroutine(MovePathByItem(moveItemInfos[index], false,
  178. () =>
  179. {
  180. if (!useAudioCtrl)
  181. {
  182. Debug.Log("动画回调");
  183. OnPlayMoveItemByIndexCallBack(index);
  184. }
  185. }));
  186. }
  187. /// <summary>
  188. /// 正向播放完成一条回调
  189. /// </summary>
  190. /// <param name="index"></param>
  191. private void OnPlayMoveItemByIndexCallBack(int index)
  192. {
  193. moveItemInfos[index].moveItem.gameObject.SetActive(false);
  194. moveItemInfos[index].modelItem.gameObject.SetActive(true);
  195. moveItemInfos[index].finish = true;
  196. if (index + 1 > moveItemInfos.Count - 1)
  197. {
  198. Debug.Log("正播播放序列完成");
  199. //UIKit.ClosePanel<AutoDisassemblyPanel>();
  200. //if (m_Reverse)
  201. //{
  202. // PlayMoveItemByReverseIndex(moveItemInfos.Count - 1);
  203. //}
  204. }
  205. else
  206. {
  207. PlayMoveItemByIndex(index + 1);
  208. }
  209. }
  210. /// <summary>
  211. /// 反向播放
  212. /// </summary>
  213. /// <param name="index"></param>
  214. private void PlayMoveItemByReverseIndex(int index)
  215. {
  216. StopAllCoroutines();
  217. Debug.Log("反向播放" + moveItemInfos[index].moveItem.name);
  218. UIKit.GetPanel<AutoDisassemblyPanel>().SetSubTitleText(moveItemInfos[index].moveItem.name);
  219. StartCoroutine(AudioHelper.LoadSpecifiedAudioFile("Config/Audios/内部结构拆解/" + deviceName, moveItemInfos[index].moveItem.name, len =>
  220. {
  221. if (len > moveItemInfos[index].moveItem.m_Duration)
  222. {
  223. Debug.Log("音频长");
  224. useAudioCtrl = true;
  225. }
  226. else
  227. {
  228. Debug.Log("动画长");
  229. useAudioCtrl = false;
  230. }
  231. },
  232. () =>
  233. {
  234. if (useAudioCtrl)
  235. {
  236. Debug.Log("音频回调");
  237. OnPlayMoveItemByReverseIndexCallBack(index);
  238. }
  239. }));
  240. StartCoroutine(MovePathByItem(moveItemInfos[index], true,
  241. () =>
  242. {
  243. if (!useAudioCtrl)
  244. {
  245. Debug.Log("动画回调");
  246. OnPlayMoveItemByReverseIndexCallBack(index);
  247. }
  248. }));
  249. }
  250. /// <summary>
  251. /// 反向播放完成一条回调
  252. /// </summary>
  253. /// <param name="index"></param>
  254. private void OnPlayMoveItemByReverseIndexCallBack(int index)
  255. {
  256. moveItemInfos[index].finish = false;
  257. if (index - 1 < 0)
  258. {
  259. Debug.Log("反向播放序列完成");
  260. //UIKit.ClosePanel<AutoDisassemblyPanel>();
  261. }
  262. else
  263. {
  264. PlayMoveItemByReverseIndex(index - 1);
  265. }
  266. }
  267. IEnumerator MovePathByItem(MoveItemInfo moveItemInfo, bool isReverse, Action finishedCallBack = null)
  268. {
  269. float timer = 0;
  270. float lerp = 0;
  271. while (timer < moveItemInfo.moveItem.m_Duration)
  272. {
  273. MoveItem moveItem = moveItemInfo.moveItem;
  274. moveItem.gameObject.SetActive(true);
  275. moveItem.OpenHighter(true);
  276. lerp = isReverse ? (1 - timer / moveItem.m_Duration) : timer / moveItem.m_Duration;
  277. if (moveItem.m_UserGlobalDirection)
  278. {
  279. moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
  280. moveItemInfo.initPosition + moveItem.m_Direction, lerp);
  281. }
  282. else
  283. {
  284. moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
  285. moveItemInfo.initPosition + moveItem.transform.TransformDirection(moveItem.m_Direction), lerp);
  286. }
  287. timer += Time.deltaTime;
  288. yield return new WaitForEndOfFrame();
  289. }
  290. moveItemInfo.moveItem?.OpenHighter(false);
  291. finishedCallBack.Invoke();
  292. }
  293. /// <summary>
  294. /// 获取所有的组件
  295. /// </summary>
  296. [TabGroup("配置相关")]
  297. [Button("获取所有的组件")]
  298. [GUIColor("GetColor")]
  299. private void GetAllMoveItems()
  300. {
  301. moveItemInfos = new List<MoveItemInfo>();
  302. MoveItem[] moveItems = transform.GetComponentsInChildren<MoveItem>();
  303. ModelItem[] modelItems = transform.GetComponentsInChildren<ModelItem>();
  304. int count = Mathf.Min(moveItems.Length, modelItems.Length);
  305. for (int i = 0; i < count; i++)
  306. {
  307. MoveItemInfo moveItemInfo = new MoveItemInfo();
  308. moveItemInfo.moveItem = moveItems[i];
  309. moveItemInfo.modelItem = modelItems[i];
  310. moveItemInfo.initPosition = moveItems[i].transform.position;
  311. moveItemInfos.Add(moveItemInfo);
  312. }
  313. //foreach (var item in moveItems)
  314. //{
  315. // MoveItemInfo moveItemInfo = new MoveItemInfo();
  316. // moveItemInfo.moveItem = item;
  317. // moveItemInfo.initPosition = item.transform.position;
  318. // moveItemInfos.Add(moveItemInfo);
  319. //}
  320. }
  321. private Color GetColor()
  322. {
  323. if (moveItemInfos.Count == 0 || moveItemInfos == null)
  324. {
  325. return Color.red;
  326. }
  327. else
  328. {
  329. return Color.green;
  330. }
  331. }
  332. /// <summary>
  333. /// 跳转并直接从指定步骤开始播放
  334. /// </summary>
  335. public void JumpToStep(int index)
  336. {
  337. if (index < 0 || index >= moveItemInfos.Count)
  338. {
  339. Debug.LogError("跳转失败:目标步骤超出范围");
  340. return;
  341. }
  342. StopAllCoroutines();
  343. AudioHelper.AudioPause();
  344. // 先全部回到初始状态
  345. SetInitState();
  346. // 前面步骤视为已完成
  347. for (int i = 0; i < index; i++)
  348. {
  349. moveItemInfos[i].finish = true;
  350. moveItemInfos[i].moveItem.gameObject.SetActive(false);
  351. moveItemInfos[i].modelItem.gameObject.SetActive(true);
  352. }
  353. // 当前步骤
  354. moveItemInfos[index].finish = false;
  355. moveItemInfos[index].moveItem.gameObject.SetActive(true);
  356. moveItemInfos[index].modelItem.gameObject.SetActive(false);
  357. // 设置字幕显示
  358. UIKit.GetPanel<AutoDisassemblyPanel>()?.SetSubTitleText(moveItemInfos[index].moveItem.name);
  359. // 时间继续推进(允许播放)
  360. Time.timeScale = 1;
  361. // 开始播放当前步骤
  362. PlayMoveItemByIndex(index);
  363. }
  364. /// <summary>
  365. /// 导出部件数据
  366. /// </summary>
  367. [TabGroup("工具相关")]
  368. [Button("1.导出所有部件名称到表格(先去根据表格配音)")]
  369. private void ExportPartData()
  370. {
  371. List<MoveData> partMarkNames = new List<MoveData>();
  372. foreach (var item in moveItemInfos)
  373. {
  374. MoveData partMarkName = new MoveData();
  375. partMarkName.moveName = item.moveItem.name;
  376. if (partMarkNames.Find(t => t.moveName == partMarkName.moveName) == null) partMarkNames.Add(partMarkName);
  377. }
  378. ExcelHelper.WriteInfoToExcel<MoveData>(Application.streamingAssetsPath + "/" + deviceName + GlobalConfig.excelSuffix, partMarkNames);
  379. }
  380. /// <summary>
  381. /// 重命名音频名称,只能重命名数字的文件
  382. /// </summary>
  383. [TabGroup("工具相关")]
  384. [Button("2.重命名音频名称(初始配音名字为数字)")]
  385. private void ReNameAudioClipName()
  386. {
  387. //表格路径
  388. string tmpExcelPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, deviceName) + GlobalConfig.excelSuffix;
  389. //音频路径完整路径信息
  390. string audioPath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, "Config/Audios/内部结构拆解/" + deviceName);
  391. List<MoveData> tmpMoveDatas = ExcelHelper.ReadInfoFromExcel<MoveData>(tmpExcelPath, 1);
  392. DirectoryInfo directoryInfo = new DirectoryInfo(audioPath);
  393. FileInfo[] files = directoryInfo.GetFiles("*.mp3");
  394. FileInfo[] sortedFiles = files.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f.FullName))).ToArray();
  395. if (tmpMoveDatas.Count != sortedFiles.Length)
  396. {
  397. Debug.LogError("重命名失败");
  398. }
  399. else
  400. {
  401. for (int i = 0; i < tmpMoveDatas.Count; i++)
  402. {
  403. string newPath = Path.Combine(directoryInfo.FullName, tmpMoveDatas[i].moveName) + ".mp3";
  404. sortedFiles[i].MoveTo(newPath);
  405. }
  406. }
  407. }
  408. /// <summary>
  409. /// 设置初始状态
  410. /// </summary>`
  411. private void SetInitState()
  412. {
  413. foreach (var info in moveItemInfos)
  414. {
  415. MoveItem moveItem = info.moveItem;
  416. ModelItem modelItem = info.modelItem;
  417. moveItem.SetInitState(info.initPosition);
  418. moveItem.OpenHighter(false);
  419. moveItem.gameObject.SetActive(true);
  420. modelItem.gameObject.SetActive(false);
  421. }
  422. }
  423. /// <summary>
  424. /// 设置最终状态
  425. /// </summary>
  426. private void SetFinalState()
  427. {
  428. foreach (var info in moveItemInfos)
  429. {
  430. MoveItem moveItem = info.moveItem;
  431. ModelItem modelItem = info.modelItem;
  432. moveItem.SetFianlState();
  433. moveItem.OpenHighter(false);
  434. moveItem.gameObject.SetActive(false);
  435. modelItem.gameObject.SetActive(true);
  436. }
  437. }
  438. public void SetModelActive(bool isActive)
  439. {
  440. foreach (var item in moveItemInfos)
  441. {
  442. item.modelItem.gameObject.SetActive(isActive);
  443. }
  444. }
  445. }
  446. [Serializable]
  447. public class MoveItemInfo
  448. {
  449. [GUIColor("FinishedStateColor")]
  450. public MoveItem moveItem;
  451. public ModelItem modelItem;
  452. [LabelText("父级对象")]
  453. /// <summary>
  454. /// 初始化位置
  455. /// </summary>
  456. [HideInInspector]
  457. public Vector3 initPosition;
  458. [LabelText("是否完成")]
  459. public bool finish;
  460. private Color FinishedStateColor()
  461. {
  462. if (finish)
  463. {
  464. return new Color(0.7f, 1f, 0.7f);
  465. }
  466. return GUI.color;
  467. }
  468. }
  469. public class MoveData
  470. {
  471. public string moveName;
  472. }