152 lines
3.7 KiB
C#
152 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
public class UavTrailRender : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 启动轨迹渲染
|
|
/// </summary>
|
|
public bool open_trailing;
|
|
/// <summary>
|
|
/// 轨迹材质
|
|
/// </summary>
|
|
public Material material;
|
|
/// <summary>
|
|
/// 轨迹起始颜色
|
|
/// </summary>
|
|
public Color color_start = Color.red;
|
|
/// <summary>
|
|
/// 轨迹终止颜色
|
|
/// </summary>
|
|
public Color color_end = Color.green;
|
|
/// <summary>
|
|
/// 轨迹起始宽度
|
|
/// </summary>
|
|
public float thinkness_start = 0.1f;
|
|
/// <summary>
|
|
/// 轨迹终止宽度
|
|
/// </summary>
|
|
public float thinkness_end = 0.1f;
|
|
|
|
/// <summary>
|
|
/// 轨迹更新位置
|
|
/// </summary>
|
|
private Vector3 pos;
|
|
/// <summary>
|
|
/// 轨迹更新频率
|
|
/// </summary>
|
|
public float frequency = 0.1f;
|
|
/// <summary>
|
|
/// 频率计时器
|
|
/// </summary>
|
|
private float timer;
|
|
|
|
private LineRenderer current_trail;
|
|
|
|
/// <summary>
|
|
/// 所有轨迹列表
|
|
/// </summary>
|
|
public List<LineRenderer> trail_list = new List<LineRenderer>();
|
|
|
|
/// <summary>
|
|
/// 自动画点,还是路径模式
|
|
/// </summary>
|
|
private bool is路径模式=false;
|
|
|
|
void Update()
|
|
{
|
|
if (open_trailing)
|
|
{
|
|
//if (!is路径模式)
|
|
{
|
|
//自动画点模式
|
|
timer += Time.deltaTime;
|
|
if (timer > frequency)
|
|
{
|
|
timer = 0;
|
|
if (pos != transform.position)
|
|
{
|
|
UpdateTrail();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (current_trail != null)
|
|
{
|
|
current_trail.startWidth = current_trail.endWidth;
|
|
current_trail = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开轨迹渲染
|
|
/// </summary>
|
|
/// <param name="_color"></param>
|
|
/// <param name="_thinkness"></param>
|
|
/// <param name="is路径模式"></param>
|
|
public void Open(Color _color, float _thinkness,bool is路径模式)
|
|
{
|
|
open_trailing = true;
|
|
this.is路径模式 = is路径模式;
|
|
color_start = color_end = _color;
|
|
thinkness_end = _thinkness;
|
|
|
|
//初始化renderlinender
|
|
if (current_trail == null)
|
|
{
|
|
InstantiateTrail();
|
|
//实例化轨迹后立即更新当前位置
|
|
if (!is路径模式)
|
|
{
|
|
UpdateTrail();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
open_trailing = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新轨迹,当前坐标自动给点
|
|
/// </summary>
|
|
public void UpdateTrail()
|
|
{
|
|
current_trail.positionCount++;
|
|
current_trail.SetPosition(current_trail.positionCount - 1, transform.position);
|
|
pos = transform.position;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新轨迹,手动给点
|
|
/// </summary>
|
|
/// <param name="potion"></param>
|
|
public void UpdateTrail(Vector3 potion)
|
|
{
|
|
current_trail.positionCount++;
|
|
current_trail.SetPosition(current_trail.positionCount - 1, potion);
|
|
pos = potion;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实例化新轨迹
|
|
/// </summary>
|
|
public void InstantiateTrail()
|
|
{
|
|
var go = new GameObject("");
|
|
go.transform.SetParent(transform);
|
|
trail_list.Add(current_trail = go.AddComponent<LineRenderer>());
|
|
current_trail.positionCount = 0;
|
|
current_trail.startWidth = thinkness_end * 1.1f;
|
|
current_trail.endWidth = thinkness_end;
|
|
if (material == null)
|
|
material = UAVManager.Instance.trail_material;
|
|
current_trail.material = material;
|
|
current_trail.startColor = color_start;
|
|
current_trail.endColor = color_end;
|
|
}
|
|
}
|