using UnityEngine;
using Vectrosity;
using System.Collections.Generic;
public class LineDraw : MonoBehaviour
{
public List<Vector3> points;
private Vector3
[] curvePoints
= new Vector3
[4];
public GameObject sourceGO;
public GameObject targetGO;
public Color lineColor = Color.red;
private Color _setColor = Color.white;
public Camera unityCamera;
//create and assign a standard material, set smoothness source to "Albedo Alpha", set smoothness to 0
public Material lineMaterial;
private Vector3 sourcePosition;
private Vector3 targetPosition;
private VectorLine myLine;
private void Start()
{
//set these items before any VectorLine is created
VectorLine.SetCamera3D(unityCamera);
//we need to set the canvas to be at the same location as the NGUI screen area
VectorLine.canvas.renderMode = RenderMode.WorldSpace;
//Change#1: Shifting canvas to sourceGO position
VectorLine.canvas.transform.position = sourceGO.transform.position;
//on UIRoot, set Constrained Scaling Style
VectorLine.canvas.scaleFactor = NGUITools.FindInParents<UIRoot>(this.gameObject).gameObject.transform.localScale.x;
if(sourceGO == null | targetGO == null)
{
Debug.LogError("Set your source and target GOs.");
return;
}
//Change#2: Changed to match sourceGO and targetGO positions
points
=new List
<Vector3
> { sourceGO
.transform.position, targetGO
.transform.position };
myLine
= new VectorLine
("myLine", points, 5f, LineType
.Continuous); //Change#3: Changed to draw straight line
// myLine.Resize(50);
//setting renderqueue > 3000 will draw in front of all NGUI objects
lineMaterial.renderQueue = 4000;
lineMaterial.SetColor("_Color", Color.red);
lineMaterial.SetColor("_EmissionColor", Color.red);
myLine.material = lineMaterial;
myLine.Draw3DAuto();
}
private void Update()
{
if(sourceGO == null | targetGO == null)
return;
if(sourceGO.transform.localPosition != sourcePosition |
targetGO.transform.localPosition != targetPosition)
{
sourcePosition = sourceGO.transform.position;
targetPosition = targetGO.transform.position;
Vector3 pos1
= new Vector3
(sourcePosition
.x, sourcePosition
.y, sourcePosition
.z); Vector3 pos2
= new Vector3
(targetPosition
.x, targetPosition
.y, targetPosition
.z);
curvePoints[0] = pos1;// sourcePosition;
curvePoints
[1] = (pos1
+ new Vector3
(0
.3f, 0f, 0f
)); curvePoints[2] = pos2;
curvePoints
[3] = (pos2
+ new Vector3
(0f, 0
.3f, 0f
));
myLine.MakeCurve(curvePoints);
}
if(_setColor != lineColor)
{
lineMaterial.renderQueue = 4000;
lineMaterial.SetColor("_Color", lineColor);
lineMaterial.SetColor("_EmissionColor", lineColor);
myLine.material = lineMaterial;
}
}
}