using System.Collections.Generic;
using UnityEngine;
public class UavTrailRender : MonoBehaviour
{
///
/// 启动轨迹渲染
///
public bool open_trailing;
///
/// 轨迹材质
///
public Material material;
///
/// 轨迹起始颜色
///
public Color color_start = Color.red;
///
/// 轨迹终止颜色
///
public Color color_end = Color.green;
///
/// 轨迹起始宽度
///
public float thinkness_start = 0.1f;
///
/// 轨迹终止宽度
///
public float thinkness_end = 0.1f;
///
/// 轨迹更新位置
///
private Vector3 pos;
///
/// 轨迹更新频率
///
public float frequency = 0.1f;
///
/// 频率计时器
///
private float timer;
private LineRenderer current_trail;
///
/// 所有轨迹列表
///
public List trail_list = new List();
///
/// 自动画点,还是路径模式
///
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;
}
}
}
///
/// 打开轨迹渲染
///
///
///
///
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;
}
///
/// 更新轨迹,当前坐标自动给点
///
public void UpdateTrail()
{
current_trail.positionCount++;
current_trail.SetPosition(current_trail.positionCount - 1, transform.position);
pos = transform.position;
}
///
/// 更新轨迹,手动给点
///
///
public void UpdateTrail(Vector3 potion)
{
current_trail.positionCount++;
current_trail.SetPosition(current_trail.positionCount - 1, potion);
pos = potion;
}
///
/// 实例化新轨迹
///
public void InstantiateTrail()
{
var go = new GameObject("");
go.transform.SetParent(transform);
trail_list.Add(current_trail = go.AddComponent());
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;
}
}