Unity實現繪製線斷二-----用GL畫矩形線框

今天有點時間,才記起來上一次寫的畫線框,接着上一節畫線,我們這節來看一下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畫線不能改變線的寬細,它是默認的,要想讓他變寬,可以考慮每兩點之間畫雙線,兩天線之間用圖片填充。
    希望大家能用的到


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章