Advertisement
Kyle_Dev

WaypointEditor - Path Drawer

Nov 3rd, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. namespace Core
  7. {
  8.     namespace WaypointEditor
  9.     {
  10.         [CustomEditor(typeof(BasicPatrolState))]
  11.         public class EnemyAIPathDrawer : Editor
  12.         {
  13.             BasicPatrolState m_AIcomponent;
  14.             float size = 1f;
  15.  
  16.             int emptyobjectcounter;
  17.  
  18.             void OnEnable()
  19.             {
  20.                 m_AIcomponent = (BasicPatrolState)target;
  21.             }
  22.  
  23.             void OnSceneGUI()
  24.             {
  25.                 Handles.color = Color.green;
  26.                 List<Transform> m_waypoints = m_AIcomponent.Path;
  27.  
  28.                 if (m_waypoints.Count > 0)
  29.                 {
  30.                     for (int trwp = 0; trwp < m_waypoints.Count; trwp++)
  31.                     {
  32.                         if (m_waypoints[trwp] != null)
  33.                         {
  34.                             Handles.color = Color.black;
  35.                             Handles.Label(new Vector3(m_waypoints[trwp].transform.position.x + 0.5f, m_waypoints[trwp].transform.position.y
  36.                             , m_waypoints[trwp].transform.position.z + 1.5f), "P_" + trwp, new GUIStyle() { fontSize = 12 });
  37.  
  38.                             Handles.color = Color.red;
  39.                             EditorGUI.BeginChangeCheck();
  40.                             Vector3 newposition = Handles.FreeMoveHandle(m_waypoints[trwp].transform.position, Quaternion.identity, size, Vector3.one * 0.5f, Handles.SphereHandleCap);
  41.                             if (EditorGUI.EndChangeCheck())
  42.                             {
  43.                                 Undo.RecordObject(m_waypoints[trwp], "Change waypoint position");
  44.                                 m_waypoints[trwp].transform.position = newposition;
  45.                             }
  46.                         }
  47.                     }
  48.                     Handles.color = Color.green;
  49.                     if (m_AIcomponent.LoopPath == true)
  50.                     {
  51.                         for (int wp = 1; wp < m_waypoints.Count + 1; wp++)
  52.                         {
  53.                             if (m_waypoints[wp - 1] != null)
  54.                             {
  55.                                 Transform previouswaypoint = m_waypoints[wp - 1];
  56.                                 Transform currentwaypoint = m_waypoints[wp % m_waypoints.Count];
  57.  
  58.                                 Handles.DrawLine(previouswaypoint.position, currentwaypoint.position);
  59.                             }
  60.                         }
  61.                     }
  62.                     else
  63.                     {
  64.                         for (int wp = 1; wp < m_waypoints.Count + 1; wp++)
  65.                         {
  66.                             if (m_waypoints[wp - 1] != null)
  67.                             {
  68.                                 Transform previouswaypoint = m_waypoints[wp - 1];
  69.                                 Transform currentwaypoint = m_waypoints[wp % m_waypoints.Count];
  70.  
  71.                                 if (wp != m_waypoints.Count)
  72.                                     Handles.DrawLine(previouswaypoint.position, currentwaypoint.position);
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.                 Handles.color = Color.white;
  78.                 HandleUtility.Repaint();
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement