AudiencePathEditor.cs 6.4 KB

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