天天看點

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類型,不然畫線會沒有顔色

Unity 畫線的方式總結
Unity 畫線的方式總結

建立一個腳本

Unity 畫線的方式總結

代碼如下:

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)));


        }

    }
}
           

效果圖如下:

Unity 畫線的方式總結
Unity 畫線的方式總結