SimpleMoveItem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Sirenix.OdinInspector;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. [ExecuteInEditMode]
  8. public class SimpleMoveItem : SerializedMonoBehaviour
  9. {
  10. [DictionaryDrawerSettings()]
  11. public Dictionary<GameObject, Transform> moveObjs = new Dictionary<GameObject, Transform>();
  12. [LabelText("相对移动位置")]
  13. public Vector3 localMovePos = new Vector3(0, 0, 0);
  14. [LabelText("相对旋转角度")]
  15. public float rotAngle = 0;
  16. [LabelText("运行时长")]
  17. public float duration = 1;
  18. public void SetInitState(bool reverse)
  19. {
  20. foreach (var item in moveObjs)
  21. {
  22. if (!reverse)
  23. {
  24. item.Key.transform.position = item.Value.position;
  25. item.Key.transform.rotation = item.Value.rotation;
  26. }
  27. else
  28. {
  29. item.Key.transform.rotation = item.Value.rotation;
  30. item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(localMovePos), rotAngle);
  31. item.Key.transform.position = item.Value.position + item.Value.TransformDirection(localMovePos);
  32. }
  33. }
  34. }
  35. public void SetFinishedState(bool reverse)
  36. {
  37. foreach (var item in moveObjs)
  38. {
  39. if (reverse)
  40. {
  41. item.Key.transform.position = item.Value.position;
  42. item.Key.transform.rotation = item.Value.rotation;
  43. }
  44. else
  45. {
  46. item.Key.transform.rotation = item.Value.rotation;
  47. item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(localMovePos), rotAngle);
  48. item.Key.transform.position = item.Value.position + item.Value.TransformDirection(localMovePos);
  49. }
  50. }
  51. }
  52. #if UNITY_EDITOR
  53. [FoldoutGroup("快捷编辑", Order = -1)]
  54. [Button("刷新物体及锚点")]
  55. public void AddSelectObj()
  56. {
  57. if (Selection.gameObjects.Length == 0) return;
  58. foreach (var item in moveObjs)
  59. {
  60. if (item.Value != null)
  61. {
  62. DestroyImmediate(item.Value.gameObject);
  63. }
  64. }
  65. moveObjs.Clear();
  66. foreach (var item in Selection.gameObjects)
  67. {
  68. GameObject tmpItem = new GameObject("锚点_" + item.gameObject.name);
  69. tmpItem.transform.parent = transform;
  70. tmpItem.transform.position = item.transform.position;
  71. tmpItem.transform.rotation = item.transform.rotation;
  72. moveObjs.Add(item, tmpItem.transform);
  73. }
  74. }
  75. [HideInInspector]
  76. public bool isShowPath = true;
  77. public bool ShowPath { get { return isShowPath; } }
  78. private void OnDrawGizmos()
  79. {
  80. DrawPathLine();
  81. }
  82. public void DrawPathLine()
  83. {
  84. if (!isShowPath) return;
  85. foreach (var item in moveObjs)
  86. {
  87. Gizmos.color = Color.yellow;
  88. Gizmos.DrawLine(item.Value.position, item.Value.position + item.Value.TransformDirection(localMovePos));
  89. }
  90. }
  91. /// <summary>
  92. /// 销毁所有子级信息
  93. /// </summary>
  94. /// <param name="item"></param>
  95. private void DestroyChild(Transform item)
  96. {
  97. if (item.childCount == 0) return;
  98. for (int i = item.childCount - 1; i >= 0; i--)
  99. {
  100. DestroyImmediate(item.GetChild(i).gameObject);
  101. }
  102. }
  103. [Button("Play")]
  104. public void Play()
  105. {
  106. isPlaying = true;
  107. timer = 0;
  108. }
  109. [HideInInspector]
  110. public bool isPlaying;
  111. private void OnEnable()
  112. {
  113. EditorApplication.update += EditorUpdate;
  114. Selection.selectionChanged += SelectionChanged;
  115. }
  116. private void OnDisable()
  117. {
  118. EditorApplication.update -= EditorUpdate;
  119. Selection.selectionChanged -= SelectionChanged;
  120. }
  121. private float timer = 0;
  122. void EditorUpdate()
  123. {
  124. if (!isPlaying) return;
  125. foreach (var item in moveObjs)
  126. {
  127. item.Key.transform.position = Vector3.Lerp(item.Value.position, item.Value.position + item.Value.TransformDirection(localMovePos), timer / duration);
  128. item.Key.transform.rotation = item.Value.rotation;
  129. item.Key.transform.RotateAround(item.Value.position, item.Value.TransformDirection(localMovePos), rotAngle * timer / duration);
  130. }
  131. timer += Time.deltaTime;
  132. if (timer > duration)
  133. {
  134. timer = 0;
  135. isPlaying = false;
  136. foreach (var item in moveObjs)
  137. {
  138. item.Key.transform.position = item.Value.position;
  139. item.Key.transform.rotation = item.Value.rotation;
  140. }
  141. }
  142. }
  143. void SelectionChanged()
  144. {
  145. if (Selection.gameObjects.Contains(this.gameObject))
  146. {
  147. isShowPath = true;
  148. }
  149. else
  150. {
  151. isShowPath = false;
  152. }
  153. }
  154. #endif
  155. }