unity linerenderer在Game窗口中任意画线

1,设置linerenderer的颜色和其它参数。

unity linerenderer在Game窗口中任意画线_第1张图片

2,新建一个空物体挂脚本。

unity linerenderer在Game窗口中任意画线_第2张图片

3,LineMark.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineMark : MonoBehaviour {



	private GameObject clone;  
	private LineRenderer line;  
	private int i;  
	public GameObject obs;  

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame  
	void Update () {  
		if (Input.GetMouseButtonDown(0))  
		{  
			clone = (GameObject)Instantiate(obs, obs.transform.position, transform.rotation);//克隆一个带有LineRender的物体   
			line = clone.GetComponent();//获得该物体上的LineRender组件  
			line.SetColors(Color.blue, Color.red);//设置颜色  
			line.SetWidth(0.2f, 0.1f);//设置宽度  
			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 linerenderer在Game窗口中任意画线_第3张图片

你可能感兴趣的:(unity,linerenderer,画线)