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

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

	}

}


 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值