unity 使用 VectorLine 画线(包含画线过程)

需要以下两个插件: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,操作更方便哦

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值