Unity 画线的方式总结

1.签名,改变不了GL画线的颜色 (shader改成 particle/additive)

 

using UnityEngine;
using System.Collections;
 
 
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;
	
    void Start()
    {
        temppos=new ArrayList();
        posAL=new  ArrayList();
    }
    void Update()
    {
        if(Input.GetMouseButton(0))
        {
             canDrawLines = true;  
        }
        if(e.type!=null &canDrawLines)  
       {  
            if(e.type == EventType.MouseDown)  
            {  
                orgPos=Input.mousePosition;  
                endPos=Input.mousePosition;  
                  
            }  
            if(e.type==EventType.MouseDrag)  
            {  
                endPos=Input.mousePosition;  
		//鼠标位置信息存入数组
                temppos.Add(Input.mousePosition);
                GLDrawLine(orgPos,endPos);  
                orgPos=Input.mousePosition;  
                print(temppos.Count);
            }  
            if(e.type==EventType.MouseUp)  
            {  
               // orgPos=Input.mousePosition;  
                endPos=Input.mousePosition;  
            }  
      }  
        
    }
    
    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,0.5f) );  
	    for(int i= 0;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();  
	    }  
	  }
	    void ClearLines()  
	  {  
	    canDrawLines = false;  
	     posAL.Clear();  
	  }
	    
	    void OnPostRender() {  
	        GLDrawLine(orgPos,endPos);  
	    }   
}

2.这个方法有点解锁的感觉

using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
    public Material mat;
    public Color color = Color.red;
    public Vector3 pos1;
    public Vector3 pos2;
    public bool isReady = false;

    void Start()
    {
        mat.color = color;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            pos1 = Input.mousePosition;
        }
        if (Input.GetMouseButtonUp(0))
        {
            pos2 = Input.mousePosition;
            isReady = true;
        }
    }

    void OnPostRender()
    {
        if (isReady)
        {
            GL.PushMatrix();
            mat.SetPass(0);
            GL.LoadOrtho();
            GL.Begin(GL.LINES);
            GL.Color(color);
            GL.Vertex3(pos1.x / Screen.width, pos1.y / Screen.height, pos1.z);
            GL.Vertex3(pos2.x / Screen.width, pos2.y / Screen.height, pos2.z);
            GL.End();
            GL.PopMatrix();
        }
    }
}

3. lineRender 画线

增加一个 LineRenderer 组建,shader类型改成 particle/additive类型,不然画线会没有颜色

新建一个脚本

代码如下:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{

    private GameObject clone;
    private LineRenderer line;
    int i;
    //带有LineRender物体
    public GameObject target;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //实例化对象
            clone = (GameObject)Instantiate(target, target.transform.position, Quaternion.identity);

            //获得该物体上的LineRender组件
            line = clone.GetComponent<LineRenderer>();
            //设置起始和结束的颜色
            line.SetColors(Color.red, Color.blue);
            //设置起始和结束的宽度
            line.SetWidth(0.4f, 0.35f);
            //计数
            i = 0;
        }
        if (Input.GetMouseButton(0))
        {
            //每一帧检测,按下鼠标的时间越长,计数越多
            i++;
            //设置顶点数
            line.SetVertexCount(i);
            //设置顶点位置(顶点的索引,将鼠标点击的屏幕坐标转换为世界坐标)
            line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));


        }

    }
}

 

效果图如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值