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

	}

}

相关文章

适配器模式将一个类的接口转换成客户期望的另一个接口,使得原本接口不兼容的类可以相互合作。
策略模式定义了一系列算法族,并封装在类中,它们之间可以互相替换,此模式让算法的变化独立于使用算法...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,它是针对软件开发中经常遇到的一些设计问题...
模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,使得子类可以在不改变算法结...
迭代器模式提供了一种方法,用于遍历集合对象中的元素,而又不暴露其内部的细节。
外观模式又叫门面模式,它提供了一个统一的(高层)接口,用来访问子系统中的一群接口,使得子系统更容...