Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.Linq;
- using Core.Waypoints;
- namespace Core
- {
- namespace WaypointEditor
- {
- public class WaypointEditor : EditorWindow
- {
- //Important for the waypoints.
- string m_pathname;
- bool m_looppath;
- //The list of enemies to edit path.
- List<BasicPatrolState> m_enemies;
- string m_selectedenemies = "";
- //Variables to help the editor.
- List<GameObject> m_createdwaypoints;
- bool m_pathcreation;
- bool m_editpath;
- int m_currentwpcount;
- int m_numberofselectedenemies;
- EditorWindow m_waypointeditorwindow;
- GameObject m_newlycreatedwaypoint;
- //Initialize the editor window.
- [MenuItem("Window/Waypoint Editor")]
- static void Init()
- {
- WaypointEditor waypointeditorwindow = (WaypointEditor)EditorWindow.GetWindow(typeof(WaypointEditor));
- }
- //Add my own on scene gui to the sceneview event so that you can do things in the scene.
- void OnEnable()
- {
- m_createdwaypoints = new List<GameObject>();
- SceneView.duringSceneGui += OnSceneGui;
- Selection.selectionChanged += AddEnemiesToList;
- m_waypointeditorwindow = GetWindow(typeof(WaypointEditor));
- Undo.undoRedoPerformed += UndoWaypoint;
- }
- //Removing own on scene gui from the sceneview event cause we don't need it if it is disabled.
- void OnDisable()
- {
- Selection.selectionChanged -= AddEnemiesToList;
- SceneView.duringSceneGui -= OnSceneGui;
- Undo.undoRedoPerformed -= UndoWaypoint;
- }
- void OnGUI()
- {
- if (m_numberofselectedenemies == 0)
- {
- 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 });
- }
- }
- //A custom function that allows us to edit the scene
- void OnSceneGui(SceneView sceneview)
- {
- Event currentevent = Event.current;
- {
- if (m_editpath == false)
- {
- //Making sure you can't select anything if you click in the scene
- if (currentevent.type == EventType.Layout)
- HandleUtility.AddDefaultControl(GUIUtility.GetControlID(GetHashCode(), FocusType.Passive));
- //Input for placing points
- PlacePoints(currentevent);
- }
- //Draw the path that is being made.
- if (m_createdwaypoints.Count > 0)
- {
- DrawPath();
- }
- m_waypointeditorwindow.Repaint();
- }
- }
- //This placed the points in the scene
- void PlacePoints(Event currentevent)
- {
- Ray ray = HandleUtility.GUIPointToWorldRay(currentevent.mousePosition);
- RaycastHit hit;
- switch (currentevent.type)
- {
- //The point is placed.
- case EventType.MouseUp:
- if (m_newlycreatedwaypoint != null)
- {
- m_newlycreatedwaypoint.transform.position = m_newlycreatedwaypoint.transform.position;
- }
- currentevent.Use();
- break;
- //Dragging allows us to change the postion of the waypoint we just created.
- case EventType.MouseDrag:
- if (Physics.Raycast(ray, out hit))
- {
- m_newlycreatedwaypoint.transform.position = hit.point;
- }
- currentevent.Use();
- break;
- //Raycast gets a point. This is where your waypoint is being made.
- case EventType.MouseDown:
- if (Physics.Raycast(ray, out hit))
- {
- InitializeWaypoint();
- m_newlycreatedwaypoint.transform.position = hit.point;
- }
- currentevent.Use();
- break;
- }
- }
- //Removing the waypoints that are undo'ed from the list and substract the currrent waypointcounting
- void UndoWaypoint()
- {
- if (m_editpath == false)
- {
- if (m_createdwaypoints.Count > 0)
- {
- m_createdwaypoints.RemoveAt(m_createdwaypoints.Count - 1);
- m_currentwpcount -= 1;
- }
- }
- }
- //Adding the enemies after the selection is changed.
- void AddEnemiesToList()
- {
- //Some cleaning purposes for reading all the enemies
- m_selectedenemies = "";
- m_numberofselectedenemies = 0;
- m_enemies = new List<BasicPatrolState>();
- //Adding everything to visualize the editting
- for (int selectedAI = 0; selectedAI < Selection.gameObjects.Length; selectedAI++)
- {
- if (Selection.gameObjects[selectedAI].GetComponentInChildren<BasicPatrolState>() != null)
- {
- BasicPatrolState ai = Selection.gameObjects[selectedAI].GetComponentInChildren<BasicPatrolState>();
- m_selectedenemies += ai.transform.parent.gameObject.name + " ";
- m_numberofselectedenemies += 1;
- m_enemies.Add(ai);
- }
- }
- m_waypointeditorwindow.Repaint();
- }
- void InitializeWaypoint()
- {
- m_newlycreatedwaypoint = new GameObject();
- Undo.RegisterCreatedObjectUndo(m_newlycreatedwaypoint, "Added a waypoint");
- m_newlycreatedwaypoint.AddComponent<Waypoint>();
- m_newlycreatedwaypoint.GetComponent<Waypoint>().WaypointAreaName = m_pathname;
- m_newlycreatedwaypoint.name = m_pathname + "_" + m_currentwpcount.ToString();
- m_createdwaypoints.Add(m_newlycreatedwaypoint);
- m_currentwpcount += 1;
- }
- //Drawing the path
- void DrawPath()
- {
- for (int wp = 0; wp < m_createdwaypoints.Count; wp++)
- {
- Handles.color = Color.red;
- if (m_editpath == false) { Handles.DrawSolidDisc(m_createdwaypoints[wp].transform.position, Vector3.up, 0.5f); }
- else
- {
- EditorGUI.BeginChangeCheck();
- Vector3 newposition = Handles.FreeMoveHandle(m_createdwaypoints[wp].transform.position, Quaternion.identity, 1f, Vector3.one * 0.5f, Handles.SphereHandleCap);
- if (EditorGUI.EndChangeCheck())
- {
- Undo.RecordObject(m_createdwaypoints[wp].transform, "Change waypoint position");
- m_createdwaypoints[wp].transform.position = newposition;
- }
- }
- Handles.color = Color.black;
- Handles.Label(new Vector3(m_createdwaypoints[wp].transform.position.x + 0.5f, m_createdwaypoints[wp].transform.position.y
- , m_createdwaypoints[wp].transform.position.z + 1.5f), "P_" + wp, new GUIStyle() { fontSize = 12 });
- }
- if (m_createdwaypoints.Count > 0)
- {
- Handles.color = Color.green;
- if (m_looppath == true)
- {
- for (int wp = 1; wp < m_createdwaypoints.Count + 1; wp++)
- {
- if (m_createdwaypoints[wp - 1] != null)
- {
- Transform previouswaypoint = m_createdwaypoints[wp - 1].transform;
- Transform currentwaypoint = m_createdwaypoints[wp % m_createdwaypoints.Count].transform;
- Handles.DrawLine(previouswaypoint.position, currentwaypoint.position);
- }
- }
- }
- else if (m_looppath == false)
- {
- for (int wp = 1; wp < m_createdwaypoints.Count + 1; wp++)
- {
- if (m_createdwaypoints[wp - 1] != null)
- {
- Transform previouswaypoint = m_createdwaypoints[wp - 1].transform;
- Transform currentwaypoint = m_createdwaypoints[wp % m_createdwaypoints.Count].transform;
- if (wp != m_createdwaypoints.Count)
- Handles.DrawLine(previouswaypoint.position, currentwaypoint.position);
- }
- }
- }
- Handles.color = Color.white;
- HandleUtility.Repaint();
- }
- }
- //Resetting some variables when we're done for re-useabllity.
- void ResetEditorVariables()
- {
- m_createdwaypoints = new List<GameObject>();
- m_currentwpcount = 0;
- m_editpath = false;
- m_pathcreation = false;
- m_looppath = false;
- m_pathname = "";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement