温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity实现绘制线断二-----用GL画矩形线框

发布时间:2020-07-15 13:53:28 来源:网络 阅读:5676 作者:速度速度撒 栏目:开发技术

今天有点时间,才记起来上一次写的画线框,接着上一节画线,我们这节来看一下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画线不能改变线的宽细,它是默认的,要想让他变宽,可以考虑每两点之间画双线,两天线之间用图片填充。
    希望大家能用的到


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI