unity用vectrosity插件画一条3D的线

时间:2022-11-07 23:21:06

vectrosity5.4.2插件下载地址:http://download.csdn.net/detail/qq_34818497/9864911

话不多说,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vectrosity;

public class MyDrawLine : MonoBehaviour {

private int m_lineName = 0;
private VectorLine m_line;
private bool m_isDraw = true;

public Texture2D m_lineStart;
public Texture2D m_lineMiddle;
public Texture2D m_lineEnd;

// Use this for initialization
void Start () {
VectorLine.SetEndCap(m_lineName.ToString(), EndCap.Both, m_lineMiddle, m_lineEnd, m_lineStart);
m_line = new VectorLine(m_lineName.ToString(), new List<Vector3>(), 20.0f, LineType.Continuous, Joins.Weld);
m_line.endCap = m_lineName.ToString();
}

// Update is called once per frame
void Update () {

if (Input.GetMouseButtonDown(1))
{
m_isDraw = !m_isDraw;
}

if (!m_isDraw)
{
if (m_line.Selected(Input.mousePosition))
{
m_line.SetColor(Color.black);
}
else if (!m_line.Selected(Input.mousePosition))
{
m_line.SetColor(Color.white);
}
////m_line.points2.Clear();
return;
}

if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
if (Physics.Raycast(ray, out hitinfo))
{
if (hitinfo.collider.gameObject.tag == "road")
{
m_line.points3.Add(hitinfo.point);
}
}

// Start off with 2 points
if (m_line.points3.Count == 1)
{
m_line.points3.Add(Vector3.zero);
}
}

// The last line point should always be where the mouse is; only draw when there are enough points
if (m_line.points3.Count >= 2)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
if (Physics.Raycast(ray, out hitinfo))
{
if (hitinfo.collider.gameObject.tag == "road")
{
m_line.points3[m_line.points3.Count - 1] = hitinfo.point;
m_line.Draw3D();
}
}
}
}
}
也可以用vectorline.SetLine3D的静态方法画3D线。