美文网首页
Unity 画线

Unity 画线

作者: 玄策丶 | 来源:发表于2018-08-01 16:51 被阅读0次

    一、使用Vectrosity插件

    1. 画一条(X0,Y0)到(X1,Y1)的线

    using Vectrosity 
    public class DrawLine:MonoBehaviour
    {
        public VectorLine energyLine;
        List<Vector2> linePoints;
        float lineWeight = 2f;
        public void LineDraw_1()
            {
                linePoints = new List<Vector2>();
                linePoints.Add(new Vector2(X0, Y0));
                linePoints.Add(new Vector2(X1, Y1));
                energyLine = new VectorLine("Energy", linePoints, lineWeight, LineType.Continuous);
                energyLine.Draw();
            }
    }
    

    如果有画折线的话,将每个折点加入List中。

    2. 正弦曲线

    using Vectrosity 
    public class DrawLine:MonoBehaviour
    {
        public VectorLine energyLine;
        List<Vector2> linePoints;
        float lineWeight = 2f;
        public void LineDraw_2()
        {
              linePoints = new List<Vector2>();
              energyLine = new VectorLine("Energy", linePoints, lineWeight, LineType.Continuous);
              for (int i = 0; i < 200; i++)
              {
              //y=Asin(ωx+φ)+k   A——振幅 (ωx+φ)——相位 φ——初相 k——偏距 ω——角速度, 控制正弦周期
                  energyLine.points2.Add(new Vector2(i , A * Mathf.Sin( ( (a * i) + b) + K) );
              }   
              energyLine.Draw();
        }
    }     
    

    删除线:

       public void Des()
        {
            List<Transform> list = new List<Transform>();
            GameObject a = GameObject.Find("VectorCanvas");
            if (a)
            {
                foreach (Transform child in a.transform)
                {
                    if (child.name.Equals("Energy"))
                    {
                        list.Add(child);
                    }               
                }
                for (int i = 0; i < list.Count; i++)
                {
                    Destroy(list[i].gameObject);
                }
            }
        }
    

    二、使用LineRenderer

    新建物体Tr0,挂载脚本drawLine,添加LineRenderer组件。Tr0移动时会画出Tr0移动的轨迹。
    PS:开始绘制的时候将脚本打开,绘制完之后关闭。不然会一直绘制,卡!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class drawLine : MonoBehaviour
    {
        private LineRenderer line;
        Vector3[] path;
        private float time = 0;
        List<Vector3> pos = new List<Vector3>();
        void Awake()
        {
            path = pos.ToArray();//初始化
            line = GetComponent<LineRenderer>();//获得该物体上的LineRender组件
        }
        void Update()
        {
            time += Time.deltaTime;
            if (time > 0.1)//每0.1秒绘制一次
            {
                time = 0;
                pos.Add(transform.position);//添加当前坐标进链表
                path = pos.ToArray();//转成数组
            }
            if (path.Length != 0)//有数据时候再绘制
            {
                line.positionCount = path.Length;//设置顶点数      
                line.SetPositions(path);//设置顶点位置
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity 画线

          本文链接:https://www.haomeiwen.com/subject/hdervftx.html