关闭

Unity3d 实现用LineRenderer画线 不依赖摄像机位置

标签: vectorcclassnull
9206人阅读 评论(4) 收藏 举报
分类:

Unity3d 实现用LineRenderer画线 不依赖摄像机位置

用LineRenderer画线难点: 鼠标坐标和屏幕坐标之间的转换   我是弄了很多天才弄好的

跟大家分享一下

using UnityEngine;

using System.Collections;

using System.Collections.Generic;



public class DrawLine: MonoBehaviour {



	private Event e;

	private int pointCnt = 0;

	public Color c1 = Color.red;

	public Color c2 = Color.blue;

	

	public GameObject obj;

	

	private LineRenderer lineRenderer;

	private LineRenderer[] lineRendArray;

	private Color[] color;

	public int maxLine = 5;

	private int index = 0;

	private bool flag = true;

	

	

	// 画线坐标之间的转换   20120330-------------

	private Vector3 screenPoint;

	private Vector3 scanPos;

	private Vector3 offset;

	//  20120330-----------------------------------



    void Start () {

	     e = Event.current; 

		 scanPos = obj.transform.position;

		 lineRenderer = (LineRenderer)obj.GetComponent("LineRenderer");

		

		// lineRenderer = (LineRenderer)gameObject.AddComponent("LineRenderer");

		 lineRenderer.material = new Material (Shader.Find("Particles/Additive"));

	     lineRenderer.SetColors(c1, c2);

	     lineRenderer.SetWidth(0.02F, 0.02F);

	     lineRenderer.SetVertexCount(0); 

		 lineRendArray = new LineRenderer[maxLine];

		 color = new Color[8];

	   	 color[0] = Color.yellow;

		 color[1] = Color.blue;

		 color[2] = Color.cyan;

		 color[3] = Color.gray;

		 color[4] = Color.green;

		 color[5] = Color.grey;

		 color[6] = Color.magenta;

		 color[7] = Color.red;

		 

    }

    

	void Update()

	{



		if(e != null){

			if (e.type == EventType.MouseDown){

	

				//////////////20120330---------------------

				////坐标之间的转换

				Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

				print("new mouse point: "+mousePos);

				 screenPoint = Camera.main.WorldToScreenPoint(scanPos);

  	    		 offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

				 Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

	 		     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); // +offset;

				//////////////20120330-----------------------

				

				if(index < maxLine){

					if(flag){

						GameObject newObj;

						newObj = (GameObject)Instantiate(obj, obj.transform.position, obj.transform.rotation);

						lineRenderer = (LineRenderer)newObj.GetComponent("LineRenderer");

						int n = Random.Range(1, 8);

						c1 = color[n-1];

						n = Random.Range(1,8);

						c2 = color[n-1];

						lineRenderer.SetColors(c1, c2);

						lineRenderer.SetWidth(0.02F,0.02F);

						lineRendArray[index] = lineRenderer;

					}

				}

				else{

					index = 0;

					flag = false;

				}

				pointCnt = 0;

				DrawRenderLine(lineRendArray[index], curPosition);

				index++;

			}

			if (e.type == EventType.MouseDrag){

				

				//////  20120330---------------------------

				Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

				print("curScreenPoint: "+curScreenPoint);

	 		    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);// + offset;

	   		

				////// 20120330-----------------------------

				if (index == 0)

					DrawRenderLine(lineRendArray[index], curPosition);

				else

					DrawRenderLine(lineRendArray[index-1], curPosition);

			}

			if (e.type == EventType.MouseUp){



			}

		}

		

		

	}



    void OnGUI () {

    	e = Event.current;

    }

	

	void DrawRenderLine(LineRenderer line, Vector3 vect3){



		Vector3 newPos = vect3;

        line.SetVertexCount(++pointCnt);

        line.SetPosition(pointCnt-1, newPos);

		print("new point: "+newPos);

	}

}


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:628456次
    • 积分:6759
    • 等级:
    • 排名:第3628名
    • 原创:53篇
    • 转载:190篇
    • 译文:3篇
    • 评论:82条
    最新评论