NewPathEditor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. [CustomEditor(typeof(NewPath))]
  5. public class NewPathEditor : Editor
  6. {
  7. public void OnSceneGUI()
  8. {
  9. NewPath _NewPath = target as NewPath;
  10. if(!_NewPath.exit)
  11. {
  12. if(Event.current.type == EventType.MouseMove) SceneView.RepaintAll();
  13. RaycastHit hit;
  14. Vector2 mPos = Event.current.mousePosition;
  15. mPos.y = Screen.height - mPos.y - 40;
  16. Ray ray = Camera.current.ScreenPointToRay(mPos);
  17. if (Physics.Raycast(ray, out hit, 3000))
  18. {
  19. _NewPath.mousePos = hit.point;
  20. if((Event.current.type == EventType.MouseDown && Event.current.button == 0))
  21. {
  22. _NewPath.PointSet(_NewPath.pointLenght, hit.point);
  23. _NewPath.pointLenght++;
  24. }
  25. }
  26. }
  27. if(Event.current.keyCode == (KeyCode.Escape))
  28. _NewPath.exit = true;
  29. }
  30. public override void OnInspectorGUI()
  31. {
  32. NewPath _NewPath = target as NewPath;
  33. EditorGUILayout.Space();
  34. if(!_NewPath.exit)
  35. {
  36. EditorGUILayout.HelpBox("Click left mouse button to create next point, esc to finish draw.", MessageType.Info);
  37. EditorGUILayout.Space();
  38. }
  39. _NewPath.pathName = EditorGUILayout.TextField("Path name: ", _NewPath.pathName);
  40. EditorGUILayout.Space();
  41. if(GUILayout.Button("Finish"))
  42. {
  43. if(!_NewPath.errors)
  44. {
  45. DestroyImmediate(_NewPath.par);
  46. _NewPath.gameObject.name = _NewPath.pathName;
  47. WalkPath wp = null;
  48. if (_NewPath.PathType == PathType.PeoplePath)
  49. {
  50. wp = _NewPath.gameObject.AddComponent<PeopleWalkPath>() as WalkPath;
  51. }
  52. else
  53. {
  54. wp = _NewPath.gameObject.AddComponent<AudiencePath>() as WalkPath;
  55. }
  56. wp.pathPoint = _NewPath.PointsGet();
  57. wp.pathType = _NewPath.PathType;
  58. GameObject _myPoints = new GameObject();
  59. _myPoints.transform.parent = _NewPath.gameObject.transform;
  60. _myPoints.name = "points";
  61. for (int i = 0; i < wp.pathPoint.Count; i++)
  62. {
  63. var _point = Instantiate(GameObject.Find("Population System").GetComponent<PopulationSystemManager>().pointPrefab, wp.pathPoint[i], Quaternion.identity) as GameObject;
  64. _point.name = "p" + i;
  65. _point.transform.parent = _myPoints.transform;
  66. wp.pathPointTransform.Add(_point);
  67. }
  68. ActiveEditorTracker.sharedTracker.isLocked = false;
  69. DestroyImmediate(_NewPath.gameObject.GetComponent<NewPath>());
  70. }
  71. }
  72. EditorGUILayout.Space();
  73. if(_NewPath.pointLenght < 2)
  74. {
  75. _NewPath.errors = true;
  76. EditorGUILayout.HelpBox("To create a path must be at least 2 points", MessageType.Warning);
  77. }
  78. if(string.IsNullOrEmpty(_NewPath.pathName))
  79. {
  80. _NewPath.errors = true;
  81. EditorGUILayout.HelpBox("Enter the path name", MessageType.Warning);
  82. }
  83. if(_NewPath.pointLenght >= 2 && !string.IsNullOrEmpty(_NewPath.pathName))
  84. _NewPath.errors = false;
  85. }
  86. }