VehicleEditor.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Traffic Simulation
  2. // https://github.com/mchrbn/unity-traffic-simulation
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace TrafficSimulation{
  6. public class VehicleEditor : Editor
  7. {
  8. [MenuItem("Component/Traffic Simulation/Setup Vehicle")]
  9. private static void SetupVehicle(){
  10. EditorHelper.SetUndoGroup("Setup Vehicle");
  11. GameObject selected = Selection.activeGameObject;
  12. //Create raycast anchor
  13. GameObject anchor = EditorHelper.CreateGameObject("Raycast Anchor", selected.transform);
  14. //Add AI scripts
  15. VehicleAI veAi = EditorHelper.AddComponent<VehicleAI>(selected);
  16. WheelDrive wheelDrive = EditorHelper.AddComponent<WheelDrive>(selected);
  17. TrafficSystem ts = GameObject.FindObjectOfType<TrafficSystem>();
  18. //Configure the vehicle AI script with created objects
  19. anchor.transform.localPosition = Vector3.zero;
  20. anchor.transform.localRotation = Quaternion.Euler(Vector3.zero);
  21. veAi.raycastAnchor = anchor.transform;
  22. if(ts != null) veAi.trafficSystem = ts;
  23. //Create layer AutonomousVehicle if it doesn't exist
  24. EditorHelper.CreateLayer("AutonomousVehicle");
  25. //Set the tag and layer name
  26. selected.tag = "AutonomousVehicle";
  27. EditorHelper.SetLayer(selected, LayerMask.NameToLayer("AutonomousVehicle"), true);
  28. }
  29. }
  30. }