加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 20:05:52 所属栏目:百科 来源:网络整理
导读:Unity3d 实现用LineRenderer画线 不依赖摄像机位置 用LineRenderer画线难点: 鼠标坐标和屏幕坐标之间的转换 我是弄了很多天才弄好的 跟大家分享一下 using UnityEngine;using System.Collections;using System.Collections.Generic;public class DrawLine:

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

	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读