今天有点时间,才记起来上一次写的画线框,接着上一节画线,我们这节来看一下GL画线
直接上代码
using UnityEngine; using System.Collections; using System.Collections.Generic; public class joint{ public Vector3 org; public Vector3 end; } public class example : MonoBehaviour { Event e; private Vector3 orgPos; private Vector3 endPos; private bool canDrawLines = false; ArrayList posAL; ArrayList temppos; public Material lineMaterial; public List <Vector3> Pos = new List<Vector3> (); void Start() { temppos=new ArrayList(); posAL=new ArrayList(); } void Update() { 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 (int i = 0; i < Pos.Count-1; i++) { Vector3 p = Pos [i]; } GLDrawLine (orgPos, endPos); orgPos = endPos; } } } 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,1f) ); for(int i= 1;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 (); } void OnGUI() { e = Event.current; if(GUI.Button(new Rect(150,0,100,50),"End Lines")) { ClearLines(); } if (GUI.Button (new Rect(10,5,100,50),"Read")) { Read (); } } void ClearLines() { canDrawLines = false; posAL.Clear(); Pos.Clear (); } void OnPostRender() { GLDrawLine(orgPos,endPos); } public void Read() { for (int i = 0; i < Pos.Count; i++) { Debug.Log (Pos [i]); } } 这就是GL画线,但是有一个问题是GL画线不能改变线的宽细,它是默认的,要想让他变宽,可以考虑每两点之间画双线,两天线之间用图片填充。 希望大家能用的到
时间: 2024-12-25 19:48:37