MoveItemManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using System;
  6. using UnityEngine.UI;
  7. public class MoveItemManager : MonoBehaviour
  8. {
  9. [TableList]
  10. public List<MoveItemInfo> moveItemInfos;
  11. [LabelText("播完反向播放")]
  12. public bool m_Reverse;
  13. public Button m_PlayButton;
  14. public Button m_CloseButton;
  15. private void Start()
  16. {
  17. m_PlayButton?.onClick.AddListener(OnPlayButtonClick);
  18. m_CloseButton?.onClick.AddListener(OnCloseBtnClick);
  19. PlayMoveItemByIndex(0);
  20. }
  21. private bool runing;
  22. private bool Runing
  23. {
  24. set
  25. {
  26. runing = value;
  27. }
  28. get
  29. {
  30. return runing;
  31. }
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="index">index 从1开始</param>
  37. public void PlayMoveItemByIndex(int index = 0)
  38. {
  39. if (index > moveItemInfos.Count - 1)
  40. {
  41. Debug.Log("正播播放序列完成");
  42. if (m_Reverse)
  43. {
  44. PlayMoveItemByReverseIndex(moveItemInfos.Count - 1);
  45. }
  46. }
  47. else
  48. {
  49. StopAllCoroutines();
  50. Debug.Log("播放" + moveItemInfos[index].moveItem.name);
  51. StartCoroutine(MovePathByItem(moveItemInfos[index], false, () =>
  52. {
  53. moveItemInfos[index].moveItem.gameObject.SetActive(false);
  54. PlayMoveItemByIndex(index + 1);
  55. }));
  56. }
  57. }
  58. public void PlayMoveItemByReverseIndex(int index)
  59. {
  60. if (index < 0)
  61. {
  62. Debug.Log("反向播放序列完成");
  63. }
  64. else
  65. {
  66. StopAllCoroutines();
  67. Debug.Log("反向播放" + moveItemInfos[index].moveItem.name);
  68. StartCoroutine(MovePathByItem(moveItemInfos[index], true, () =>
  69. {
  70. PlayMoveItemByReverseIndex(index - 1);
  71. }));
  72. }
  73. }
  74. IEnumerator MovePathByItem(MoveItemInfo moveItemInfo, bool isReverse, Action finishedCallBack = null)
  75. {
  76. float timer = 0;
  77. float lerp = 0;
  78. while (timer < moveItemInfo.moveItem.m_Duration)
  79. {
  80. MoveItem moveItem = moveItemInfo.moveItem;
  81. moveItem.gameObject.SetActive(true);
  82. moveItem.OpenHighter(true);
  83. lerp = isReverse? (1 - timer / moveItem.m_Duration) :timer / moveItem.m_Duration;
  84. if (moveItem.m_UserGlobalDirection)
  85. {
  86. moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
  87. moveItemInfo.initPosition + moveItem.m_Direction, lerp);
  88. }
  89. else
  90. {
  91. moveItemInfo.moveItem.transform.position = Vector3.Lerp(moveItemInfo.initPosition,
  92. moveItemInfo.initPosition + moveItem.transform.TransformDirection(moveItem.m_Direction), lerp);
  93. }
  94. timer += Time.deltaTime;
  95. yield return new WaitForEndOfFrame();
  96. }
  97. moveItemInfo.finish = true;
  98. moveItemInfo.moveItem?.OpenHighter(false);
  99. finishedCallBack.Invoke();
  100. }
  101. /// <summary>
  102. /// 获取所有的组件
  103. /// </summary>
  104. [Button("获取所有的组件")]
  105. private void GetAllMoveItems()
  106. {
  107. moveItemInfos = new List<MoveItemInfo>();
  108. MoveItem[] moveItems = transform.GetComponentsInChildren<MoveItem>();
  109. foreach (var item in moveItems)
  110. {
  111. MoveItemInfo moveItemInfo = new MoveItemInfo();
  112. moveItemInfo.moveItem = item;
  113. moveItemInfo.initPosition = item.transform.position;
  114. moveItemInfos.Add(moveItemInfo);
  115. }
  116. }
  117. /// <summary>
  118. /// 播放按钮点击
  119. /// </summary>
  120. private void OnPlayButtonClick()
  121. {
  122. if (Time.timeScale == 0)
  123. {
  124. Time.timeScale = 1;
  125. if (m_PlayButton)
  126. {
  127. m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "暂停";
  128. }
  129. }
  130. else
  131. {
  132. Time.timeScale = 0;
  133. if (m_PlayButton)
  134. {
  135. m_PlayButton.transform.Find("Text").GetComponent<Text>().text = "播放";
  136. }
  137. }
  138. }
  139. private void OnCloseBtnClick()
  140. {
  141. Application.Quit();
  142. }
  143. }
  144. [Serializable]
  145. public class MoveItemInfo
  146. {
  147. [GUIColor("FinishedStateColor")]
  148. public MoveItem moveItem;
  149. [LabelText("父级对象")]
  150. /// <summary>
  151. /// 初始化位置
  152. /// </summary>
  153. [HideInInspector]
  154. public Vector3 initPosition;
  155. [LabelText("是否完成")]
  156. public bool finish;
  157. private Color FinishedStateColor()
  158. {
  159. if (finish)
  160. {
  161. return new Color(0.7f, 1f, 0.7f);
  162. }
  163. return GUI.color;
  164. }
  165. }