89 lines
3.3 KiB
C#
89 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 无人机规划路径
|
|
/// </summary>
|
|
public class ObjectPlanner : MonoBehaviour
|
|
{
|
|
public bool isPathCanBePlanned = false;//是否可以规划路径
|
|
public Camera camera; // Orthographic相机
|
|
public GameObject selectedObject; // 被选择的物体
|
|
public LineRenderer lineRenderer; // 用于绘制路线的线条
|
|
private bool isPlanning = false; // 是否正在规划路线
|
|
public UnmannedAerialVehicleManage unmannedAerialVehicleManage;
|
|
|
|
void Start()
|
|
{
|
|
if (camera == null)
|
|
camera = Camera.main;
|
|
lineRenderer = GetComponent<LineRenderer>(); // 获取LineRenderer组件
|
|
|
|
// 设置线条材质和宽度
|
|
//lineRenderer.material = new Material(Shader.Find("Sprites/Default")); // 设置线条材质
|
|
lineRenderer.startWidth = 10f; // 设置线条起始宽度
|
|
lineRenderer.endWidth =10f; // 设置线条结束宽度
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (isPathCanBePlanned)
|
|
{
|
|
if (Input.GetMouseButtonDown(1)) // 检测鼠标左键是否按下
|
|
{
|
|
Ray ray = camera.ScreenPointToRay(Input.mousePosition); // 从相机发射一条射线
|
|
if (Physics.Raycast(ray, out RaycastHit hit)) // 检测是否射中物体
|
|
{
|
|
if (selectedObject != null && !isPlanning) // 如果已选择物体并且没有正在规划路线
|
|
{
|
|
isPlanning = true; // 标记为正在规划路线
|
|
|
|
lineRenderer.positionCount = 1; // 设置线条的起点
|
|
lineRenderer.SetPosition(0, selectedObject.transform.position);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if (isPlanning && Input.GetMouseButton(1)) // 如果正在规划路线并且鼠标左键持续按下
|
|
{
|
|
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
|
|
if (Physics.Raycast(ray, out RaycastHit hit))
|
|
{
|
|
lineRenderer.positionCount++; // 增加线条顶点数
|
|
lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point); // 更新线条的顶点
|
|
}
|
|
}
|
|
|
|
if (isPlanning && Input.GetMouseButtonUp(1)) // 如果正在规划路线并且鼠标左键释放
|
|
{
|
|
isPlanning = false; // 停止规划路线
|
|
|
|
//Vector3[] positions = new Vector3[lineRenderer.positionCount]; // 创建用于存储顶点坐标的数组
|
|
//lineRenderer.GetPositions(positions); // 获取线条的顶点坐标
|
|
//if (unmannedAerialVehicleManage)
|
|
//{
|
|
// for (int i = 0; i < positions.Length; i++)
|
|
// {
|
|
// unmannedAerialVehicleManage.positions.Enqueue(positions[i]);
|
|
// unmannedAerialVehicleManage.endPosition = positions[positions.Length - 1];
|
|
// }
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置选定无人机编队
|
|
/// </summary>
|
|
/// <param name="_selectedObject"></param>
|
|
public void SetSelectedObject(GameObject _selectedObject)
|
|
{
|
|
selectedObject = _selectedObject; // 选定新的物体
|
|
}
|
|
|
|
|
|
|
|
}
|