需要以下两个插件:Vectrosity和DOTween
将下面脚本挂在要绘制连线的对象的父物体上,lineMaterial材质球使用Vectrosity插件自带的SolidColor Shader
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vectrosity;
using DG.Tweening;
public class DawnLine : MonoBehaviour
{
public bool is3D = false;
private List<Transform> pos;
private VectorLine line;
public float time = 0.2f;
public float width = 5f;
//public Color color = Color.green;
public Material lineMaterial;
private int index = 0;
private Vector3 tmpVec3;
private bool isDraw = false;
void Start()
{
InitLine();
}
private void InitLine()
{
pos = new List<Transform>();
for (int i = 0; i < transform.childCount; i++)
{
pos.Add(transform.GetChild(i));
}
if (is3D)
{
line = new VectorLine("line", new List<Vector3>(), width, LineType.Continuous, Joins.Weld);
}
else
{
line = new VectorLine("line", new List<Vector2>(), width, LineType.Continuous, Joins.Weld);
}
//line.color = color;
line.material = lineMaterial;
}
public void StartDawnLine()
{
HideDrawLine();
tmpVec3 = pos[0].position;
Draw();
StartCoroutine("Wait");
}
public void HideDrawLine()
{
if (isDraw)
{
return;
}
if(is3D)
{
line.points3.Clear();
}
else
{
line.points2.Clear();
}
line.Draw();
index = 0;
StopCoroutine("Wait");
}
IEnumerator Wait()
{
isDraw = true;
while(index < pos.Count - 1)
{
if (index == pos.Count - 2)
{
DOTween.To(() => pos[index].position, x => tmpVec3 = x, pos[index + 1].position, time).OnUpdate(Draw).OnComplete(EndDrawLine).SetEase(Ease.Linear);
}
else
{
DOTween.To(() => pos[index].position, x => tmpVec3 = x, pos[index + 1].position, time).OnUpdate(Draw).SetEase(Ease.Linear);
}
yield return new WaitForSeconds(time);
index++;
}
yield break;
}
private void Draw()
{
if(is3D)
{
line.points3.Add(tmpVec3);
line.Draw3D();
}
else
{
line.points2.Add(tmpVec3);
line.Draw();
}
}
private void EndDrawLine()
{
//Debug.Log("draw end");
isDraw = false;
}
}
场景看起来就像这样
运行效果如下
最后,如不嫌弃,小小工程献上
链接: https://pan.baidu.com/s/1wi-BjpNWdRlVCmTGY1plfw 提取码: icep 复制这段内容后打开百度网盘手机App,操作更方便哦