Advertisement
Kyle_Dev

WaypointEditor - The Waypoint Editor

Nov 4th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Linq;
  6. using Core.Waypoints;
  7.  
  8. namespace Core
  9. {
  10.     namespace WaypointEditor
  11.     {
  12.         public class WaypointEditor : EditorWindow
  13.         {
  14.             //Important for the waypoints.
  15.             string m_pathname;
  16.             bool m_looppath;
  17.  
  18.             //The list of enemies to edit path.
  19.             List<BasicPatrolState> m_enemies;
  20.             string m_selectedenemies = "";
  21.  
  22.             //Variables to help the editor.
  23.             List<GameObject> m_createdwaypoints;
  24.             bool m_pathcreation;
  25.             bool m_editpath;
  26.             int m_currentwpcount;
  27.             int m_numberofselectedenemies;
  28.             EditorWindow m_waypointeditorwindow;
  29.             GameObject m_newlycreatedwaypoint;
  30.  
  31.             //Initialize the editor window.
  32.             [MenuItem("Window/Waypoint Editor")]
  33.             static void Init()
  34.             {
  35.                 WaypointEditor waypointeditorwindow = (WaypointEditor)EditorWindow.GetWindow(typeof(WaypointEditor));
  36.             }
  37.  
  38.             //Add my own on scene gui to the sceneview event so that you can do things in the scene.
  39.             void OnEnable()
  40.             {
  41.                 m_createdwaypoints = new List<GameObject>();
  42.                 SceneView.duringSceneGui += OnSceneGui;
  43.                 Selection.selectionChanged += AddEnemiesToList;
  44.                 m_waypointeditorwindow = GetWindow(typeof(WaypointEditor));
  45.                 Undo.undoRedoPerformed += UndoWaypoint;
  46.             }
  47.  
  48.             //Removing own on scene gui from the sceneview event cause we don't need it if it is disabled.
  49.             void OnDisable()
  50.             {
  51.                 Selection.selectionChanged -= AddEnemiesToList;
  52.                 SceneView.duringSceneGui -= OnSceneGui;
  53.                 Undo.undoRedoPerformed -= UndoWaypoint;
  54.             }
  55.  
  56.  
  57.             void OnGUI()
  58.             {
  59.                 if (m_numberofselectedenemies == 0)
  60.                 {
  61.                     GUILayout.Label("Select an Enemy or Enemies to give a path to \n(You can find them in the hiearchy)", new GUIStyle { fontSize = 16, alignment = TextAnchor.MiddleCenter });
  62.                 }
  63.             }
  64.  
  65.             //A custom function that allows us to edit the scene
  66.             void OnSceneGui(SceneView sceneview)
  67.             {
  68.                 Event currentevent = Event.current;
  69.                 {
  70.                     if (m_editpath == false)
  71.                     {
  72.                         //Making sure you can't select anything if you click in the scene
  73.                         if (currentevent.type == EventType.Layout)
  74.                             HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
  75.  
  76.                         //Input for placing points
  77.                         PlacePoints(currentevent);
  78.                     }
  79.  
  80.                     //Draw the path that is being made.
  81.                     if (m_createdwaypoints.Count > 0)
  82.                     {
  83.                         DrawPath();
  84.                     }
  85.  
  86.                     m_waypointeditorwindow.Repaint();
  87.                 }
  88.             }
  89.  
  90.             //This placed the points in the scene
  91.             void PlacePoints(Event currentevent)
  92.             {
  93.                 Ray ray = HandleUtility.GUIPointToWorldRay(currentevent.mousePosition);
  94.                 RaycastHit hit;
  95.  
  96.                 switch (currentevent.type)
  97.                 {
  98.                     //The point is placed.
  99.                     case EventType.MouseUp:
  100.                         if (m_newlycreatedwaypoint != null)
  101.                         {
  102.                             m_newlycreatedwaypoint.transform.position = m_newlycreatedwaypoint.transform.position;
  103.                         }
  104.                         currentevent.Use();
  105.                         break;
  106.  
  107.                     //Dragging allows us to change the postion of the waypoint we just created.
  108.                     case EventType.MouseDrag:
  109.                         if (Physics.Raycast(ray, out hit))
  110.                         {
  111.                             m_newlycreatedwaypoint.transform.position = hit.point;
  112.                         }
  113.                         currentevent.Use();
  114.                         break;
  115.  
  116.                     //Raycast gets a point. This is where your waypoint is being made.
  117.                     case EventType.MouseDown:
  118.                         if (Physics.Raycast(ray, out hit))
  119.                         {
  120.                             InitializeWaypoint();
  121.                             m_newlycreatedwaypoint.transform.position = hit.point;
  122.                         }
  123.                         currentevent.Use();
  124.                         break;
  125.                 }
  126.             }
  127.  
  128.             //Removing the waypoints that are undo'ed from the list and substract the currrent waypointcounting
  129.             void UndoWaypoint()
  130.             {
  131.                 if (m_editpath == false)
  132.                 {
  133.                     if (m_createdwaypoints.Count > 0)
  134.                     {
  135.                         m_createdwaypoints.RemoveAt(m_createdwaypoints.Count - 1);
  136.                         m_currentwpcount -= 1;
  137.                     }
  138.                 }
  139.             }
  140.  
  141.             //Adding the enemies after the selection is changed.
  142.             void AddEnemiesToList()
  143.             {
  144.                 //Some cleaning purposes for reading all the enemies
  145.                 m_selectedenemies = "";
  146.                 m_numberofselectedenemies = 0;
  147.                 m_enemies = new List<BasicPatrolState>();
  148.  
  149.                 //Adding everything to visualize the editting
  150.                 for (int selectedAI = 0; selectedAI < Selection.gameObjects.Length; selectedAI++)
  151.                 {
  152.                     if (Selection.gameObjects[selectedAI].GetComponentInChildren<BasicPatrolState>() != null)
  153.                     {
  154.                         BasicPatrolState ai = Selection.gameObjects[selectedAI].GetComponentInChildren<BasicPatrolState>();
  155.                         m_selectedenemies += ai.transform.parent.gameObject.name + " ";
  156.                         m_numberofselectedenemies += 1;
  157.                         m_enemies.Add(ai);
  158.                     }
  159.                 }
  160.  
  161.                 m_waypointeditorwindow.Repaint();
  162.             }
  163.  
  164.             void InitializeWaypoint()
  165.             {
  166.                 m_newlycreatedwaypoint = new GameObject();
  167.                 Undo.RegisterCreatedObjectUndo(m_newlycreatedwaypoint, "Added a waypoint");
  168.                 m_newlycreatedwaypoint.AddComponent<Waypoint>();
  169.                 m_newlycreatedwaypoint.GetComponent<Waypoint>().WaypointAreaName = m_pathname;
  170.                 m_newlycreatedwaypoint.name = m_pathname + "_" + m_currentwpcount.ToString();
  171.                 m_createdwaypoints.Add(m_newlycreatedwaypoint);
  172.                 m_currentwpcount += 1;
  173.             }
  174.  
  175.              //Drawing the path
  176.             void DrawPath()
  177.             {
  178.                 for (int wp = 0; wp < m_createdwaypoints.Count; wp++)
  179.                 {
  180.                     Handles.color = Color.red;
  181.                     if (m_editpath == false) { Handles.DrawSolidDisc(m_createdwaypoints[wp].transform.position, Vector3.up, 0.5f); }
  182.                     else
  183.                     {
  184.                         EditorGUI.BeginChangeCheck();
  185.                         Vector3 newposition = Handles.FreeMoveHandle(m_createdwaypoints[wp].transform.position, Quaternion.identity, 1f, Vector3.one * 0.5f, Handles.SphereHandleCap);
  186.                         if (EditorGUI.EndChangeCheck())
  187.                         {
  188.                             Undo.RecordObject(m_createdwaypoints[wp].transform, "Change waypoint position");
  189.                             m_createdwaypoints[wp].transform.position = newposition;
  190.                         }
  191.                     }
  192.  
  193.                     Handles.color = Color.black;
  194.                     Handles.Label(new Vector3(m_createdwaypoints[wp].transform.position.x + 0.5f, m_createdwaypoints[wp].transform.position.y
  195.                         , m_createdwaypoints[wp].transform.position.z + 1.5f), "P_" + wp, new GUIStyle() { fontSize = 12 });
  196.                 }
  197.  
  198.                 if (m_createdwaypoints.Count > 0)
  199.                 {
  200.                     Handles.color = Color.green;
  201.                     if (m_looppath == true)
  202.                     {
  203.                         for (int wp = 1; wp < m_createdwaypoints.Count + 1; wp++)
  204.                         {
  205.                             if (m_createdwaypoints[wp - 1] != null)
  206.                             {
  207.                                 Transform previouswaypoint = m_createdwaypoints[wp - 1].transform;
  208.                                 Transform currentwaypoint = m_createdwaypoints[wp % m_createdwaypoints.Count].transform;
  209.  
  210.                                 Handles.DrawLine(previouswaypoint.position, currentwaypoint.position);
  211.                             }
  212.                         }
  213.                     }
  214.                     else if (m_looppath == false)
  215.                     {
  216.                         for (int wp = 1; wp < m_createdwaypoints.Count + 1; wp++)
  217.                         {
  218.                             if (m_createdwaypoints[wp - 1] != null)
  219.                             {
  220.                                 Transform previouswaypoint = m_createdwaypoints[wp - 1].transform;
  221.                                 Transform currentwaypoint = m_createdwaypoints[wp % m_createdwaypoints.Count].transform;
  222.  
  223.                                 if (wp != m_createdwaypoints.Count)
  224.                                     Handles.DrawLine(previouswaypoint.position, currentwaypoint.position);
  225.                             }
  226.                         }
  227.                     }
  228.                     Handles.color = Color.white;
  229.  
  230.                     HandleUtility.Repaint();
  231.                 }
  232.             }
  233.  
  234.             //Resetting some variables when we're done for re-useabllity.
  235.             void ResetEditorVariables()
  236.             {
  237.                 m_createdwaypoints = new List<GameObject>();
  238.                 m_currentwpcount = 0;
  239.                 m_editpath = false;
  240.                 m_pathcreation = false;
  241.                 m_looppath = false;
  242.                 m_pathname = "";
  243.             }
  244.         }
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement