对某篇文章加以修改。把这个脚本挂到相机下,才能显示。
using UnityEngine;
using System.Collections;
public class joint{
public Vector3 org;
public Vector3 end;
}
public class Drawline : MonoBehaviour {
private Vector3 orgPos;
private Vector3 endPos;
private bool canDrawLines = false;
ArrayList posAL;
ArrayList temppos;
public Material lineMaterial;
void Start()
{
temppos=new ArrayList();
posAL=new ArrayList();
}
void Update()
{
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began))
{
Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D lastHit = Physics2D.Raycast(inputPos, new Vector2(0, 0), 0.1f, LayerMask.GetMask("Drawline"));
if (lastHit.collider != null)
{
orgPos=Input.mousePosition;
endPos=Input.mousePosition;
}
}
else if (Input.GetMouseButton(0) || (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Moved))
{
Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D lastHit = Physics2D.Raycast(inputPos, new Vector2(0, 0), 0.1f, LayerMask.GetMask("Drawline"));
if (lastHit.collider != null)
{
canDrawLines = true;
endPos=Input.mousePosition;
temppos.Add(Input.mousePosition);
GLDrawLine(orgPos,endPos);
orgPos=Input.mousePosition;
}
}
else if(Input.GetMouseButtonUp(0) || (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Ended))
{
Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D lastHit = Physics2D.Raycast(inputPos, new Vector2(0, 0), 0.1f, LayerMask.GetMask("Drawline"));
if (lastHit.collider != null)
{
endPos=Input.mousePosition;
}
}
}
void GLDrawLine(Vector3 beg ,Vector3 end )
{
if(!canDrawLines)
return;
GL.PushMatrix ();
GL.LoadOrtho ();
beg.x=beg.x/Screen.width;
end.x=end.x/Screen.width;
beg.y=beg.y/Screen.height;
end.y=end.y/Screen.height;
joint tmpJoint = new joint();
tmpJoint.org=beg;
tmpJoint.end=end;
posAL.Add(tmpJoint);
lineMaterial.SetPass( 0 );
GL.Begin( GL.LINES );
GL.Color( new Color(1,1,1,0.5f) );
for(int i= 0;i<posAL.Count;i++)
{
joint tj =(joint)posAL[i];
Vector3 tmpBeg = tj.org;
Vector3 tmpEnd=tj.end;
GL.Vertex3( tmpBeg.x,tmpBeg.y,tmpBeg.z );
GL.Vertex3( tmpEnd.x,tmpEnd.y,tmpEnd.z );
}
GL.End();
GL.PopMatrix ();
}
public void ClearLines()
{
canDrawLines = false;
if(posAL != null)
posAL.Clear();
}
void OnPostRender() {
GLDrawLine(orgPos,endPos);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。