今天有点时间,才记起来上一次写的画线框,接着上一节画线,我们这节来看一下GL画线

直接上代码

usingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;publicclassjoint{publicVector3org;publicVector3end;}publicclassexample:MonoBehaviour{Evente;privateVector3orgPos;privateVector3endPos;privateboolcanDrawLines=false;ArrayListposAL;ArrayListtemppos;publicMateriallineMaterial;publicList<Vector3>Pos=newList<Vector3>();voidStart(){temppos=newArrayList();posAL=newArrayList();}voidUpdate(){if(Input.GetMouseButtonUp(0)){canDrawLines=true;}if(e.type!=null&canDrawLines){if(e.type==EventType.MouseDown){orgPos=Input.mousePosition;}if(e.type==EventType.MouseUp){endPos=Input.mousePosition;Pos.Add(endPos);for(inti=0;i<Pos.Count-1;i++){Vector3p=Pos[i];}GLDrawLine(orgPos,endPos);orgPos=endPos;}}}voidGLDrawLine(Vector3beg,Vector3end){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;jointtmpJoint=newjoint();tmpJoint.org=beg;tmpJoint.end=end;posAL.Add(tmpJoint);lineMaterial.SetPass(0);GL.Begin(GL.LINES);GL.Color(newColor(1,1,1,1f));for(inti=1;i<posAL.Count;i++){jointtj=(joint)posAL[i];Vector3tmpBeg=tj.org;Vector3tmpEnd=tj.end;GL.Vertex3(tmpBeg.x,tmpBeg.y,tmpBeg.z);GL.Vertex3(tmpEnd.x,tmpEnd.y,tmpEnd.z);}GL.End();GL.PopMatrix();}voidOnGUI(){e=Event.current;if(GUI.Button(newRect(150,0,100,50),"EndLines")){ClearLines();}if(GUI.Button(newRect(10,5,100,50),"Read")){Read();}}voidClearLines(){canDrawLines=false;posAL.Clear();Pos.Clear();}voidOnPostRender(){GLDrawLine(orgPos,endPos);}publicvoidRead(){for(inti=0;i<Pos.Count;i++){Debug.Log(Pos[i]);}}这就是GL画线,但是有一个问题是GL画线不能改变线的宽细,它是默认的,要想让他变宽,可以考虑每两点之间画双线,两天线之间用图片填充。希望大家能用的到