PeopleWalkPathEditor.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.IO;
  5. [CustomEditor(typeof(PeopleWalkPath))]
  6. public class PeopleWalkPathEditor : Editor
  7. {
  8. private WalkPath walkPathTarget;
  9. private SerializedProperty _moveTypeProperty;
  10. private SerializedProperty _walkSpeedProperty;
  11. private SerializedProperty _runSpeedProperty;
  12. private SerializedProperty _overrideDefaultAnimationMultiplierProperty;
  13. private SerializedProperty _customWalkAnimationMultiplierProperty;
  14. private SerializedProperty _customRunAnimationMultiplierProperty;
  15. public static List<T> FindAssetsByType<T>() where T : UnityEngine.Object
  16. {
  17. List<T> assets = new List<T>();
  18. string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(T)));
  19. for (int i = 0; i < guids.Length; i++)
  20. {
  21. string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
  22. T asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
  23. if (asset != null)
  24. {
  25. assets.Add(asset);
  26. }
  27. }
  28. return assets;
  29. }
  30. public void OnEnable()
  31. {
  32. walkPathTarget = target as WalkPath;
  33. _moveTypeProperty = serializedObject.FindProperty("_moveType");
  34. _walkSpeedProperty = serializedObject.FindProperty("walkSpeed");
  35. _runSpeedProperty = serializedObject.FindProperty("runSpeed");
  36. _overrideDefaultAnimationMultiplierProperty = serializedObject.FindProperty("_overrideDefaultAnimationMultiplier");
  37. _customWalkAnimationMultiplierProperty = serializedObject.FindProperty("_customWalkAnimationMultiplier");
  38. _customRunAnimationMultiplierProperty = serializedObject.FindProperty("_customRunAnimationMultiplier");
  39. }
  40. public List<DirectoryInfo> FindAllDirs(string path)
  41. {
  42. List<DirectoryInfo> ret = new List<DirectoryInfo>();
  43. DirectoryInfo f = new DirectoryInfo(path);
  44. if (f.GetDirectories().Length > 0)
  45. {
  46. foreach (var item in f.GetDirectories())
  47. {
  48. ret.Add(item);
  49. if (item.GetDirectories().Length > 0)
  50. {
  51. ret.AddRange(FindAllDirs(item.FullName));
  52. }
  53. }
  54. }
  55. return ret;
  56. }
  57. public void OnSceneGUI()
  58. {
  59. if (walkPathTarget.newPointCreation || walkPathTarget.oldPointDeleting)
  60. {
  61. if (Event.current.type == EventType.MouseMove) SceneView.RepaintAll();
  62. RaycastHit hit;
  63. Vector2 mPos = Event.current.mousePosition;
  64. mPos.y = Screen.height - mPos.y - 40;
  65. Ray ray = Camera.current.ScreenPointToRay(mPos);
  66. if (Physics.Raycast(ray, out hit, 3000))
  67. {
  68. walkPathTarget.mousePosition = hit.point;
  69. if ((Event.current.type == EventType.MouseDown && Event.current.button == 0))
  70. {
  71. // создаём новую точку
  72. if (walkPathTarget.newPointCreation)
  73. {
  74. walkPathTarget.AddPoint();
  75. }
  76. // удаляем старую точку
  77. if (walkPathTarget.oldPointDeleting)
  78. {
  79. walkPathTarget.DeletePoint();
  80. }
  81. }
  82. }
  83. }
  84. }
  85. public override void OnInspectorGUI()
  86. {
  87. serializedObject.Update();
  88. WalkPath walkPath = target as WalkPath;
  89. EditorGUILayout.Space();
  90. GUIStyle boxStyle = new GUIStyle("Box");
  91. EditorGUILayout.LabelField("People Walking Path", boxStyle, GUILayout.ExpandWidth(true));
  92. EditorGUILayout.Space();
  93. DrawDefaultInspector();
  94. EditorGUILayout.Space();
  95. EditorGUILayout.PropertyField(_moveTypeProperty, new GUIContent("Move Type"));
  96. if (_moveTypeProperty.enumValueIndex == 0)
  97. {
  98. EditorGUILayout.PropertyField(_walkSpeedProperty, new GUIContent("Walk Speed"));
  99. }
  100. else
  101. {
  102. EditorGUILayout.PropertyField(_runSpeedProperty, new GUIContent("Run Speed"));
  103. }
  104. EditorGUILayout.PropertyField(_overrideDefaultAnimationMultiplierProperty, new GUIContent("Override Animation Multiplier"));
  105. if (_overrideDefaultAnimationMultiplierProperty.boolValue)
  106. {
  107. EditorGUILayout.PropertyField(_customWalkAnimationMultiplierProperty, new GUIContent("Custom walk animation speed"));
  108. EditorGUILayout.PropertyField(_customRunAnimationMultiplierProperty, new GUIContent("Custom run animation speed"));
  109. }
  110. if (_overrideDefaultAnimationMultiplierProperty.boolValue) EditorGUILayout.Space();
  111. EditorGUILayout.Space();
  112. GUI.backgroundColor = Color.green;
  113. if (GUILayout.Button("Populate!"))
  114. {
  115. if (walkPath.par != null)
  116. {
  117. DestroyImmediate(walkPath.par);
  118. }
  119. if (walkPath.peoplePrefabs != null && walkPath.peoplePrefabs.Length > 0 && walkPath.peoplePrefabs[0] != null)
  120. {
  121. walkPath.SpawnPeople();
  122. }
  123. }
  124. GUI.backgroundColor = Color.white;
  125. if (GUILayout.Button("Remove prefabs"))
  126. {
  127. if (walkPath.par != null)
  128. {
  129. DestroyImmediate(walkPath.par);
  130. }
  131. }
  132. EditorGUILayout.Space();
  133. EditorGUILayout.Space();
  134. EditorGUILayout.Space();
  135. if (walkPath.peoplePrefabs == null || walkPath.peoplePrefabs.Length == 0 || walkPath.peoplePrefabs[0] == null)
  136. EditorGUILayout.HelpBox("To create a path must be at least 1 walking object prefab", MessageType.Warning);
  137. if ((walkPathTarget.oldPointDeleting ||
  138. walkPathTarget.newPointCreation) &&
  139. GUILayout.Button("Edit Points Finish"))
  140. {
  141. walkPathTarget.newPointCreation = false;
  142. walkPathTarget.oldPointDeleting = false;
  143. walkPathTarget.EditorLock(false);
  144. }
  145. if (!walkPathTarget.newPointCreation &&
  146. !walkPathTarget.oldPointDeleting)
  147. {
  148. if (GUILayout.Button("Add Points"))
  149. {
  150. walkPathTarget.newPointCreation = true;
  151. walkPathTarget.EditorLock(true);
  152. }
  153. if (GUILayout.Button("Delete Points"))
  154. {
  155. walkPathTarget.oldPointDeleting = true;
  156. walkPathTarget.EditorLock(true);
  157. }
  158. if (!walkPath.disableLineDraw)
  159. {
  160. if (GUILayout.Button("HIDE GRAPHICS"))
  161. {
  162. walkPath.disableLineDraw = true;
  163. walkPath.HideExistingIcons();
  164. return;
  165. }
  166. }
  167. if (walkPath.disableLineDraw)
  168. {
  169. if (GUILayout.Button("SHOW GRAPHICS"))
  170. {
  171. walkPath.disableLineDraw = false;
  172. walkPath.ShowExistingIcons();
  173. }
  174. }
  175. }
  176. if (GUILayout.Button("Re-Build Points"))
  177. {
  178. Transform parentOfPoints = walkPath.transform.Find("points");
  179. Transform[] pointsTransform = parentOfPoints.GetComponentsInChildren<Transform>();
  180. walkPath.pathPoint.Clear();
  181. walkPath.pathPointTransform.Clear();
  182. for (int i = 1; i < pointsTransform.Length; i++)
  183. {
  184. walkPath.pathPoint.Add(pointsTransform[i].position);
  185. walkPath.pathPointTransform.Add(pointsTransform[i].gameObject);
  186. }
  187. }
  188. serializedObject.ApplyModifiedProperties();
  189. }
  190. }