您好,登錄后才能下訂單哦!
今天有點時間,才記起來上一次寫的畫線框,接著上一節畫線,我們這節來看一下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畫線不能改變線的寬細,它是默認的,要想讓他變寬,可以考慮每兩點之間畫雙線,兩天線之間用圖片填充。 希望大家能用的到
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。