using UnityEngine;
using System.Collections;
/// <summary>
/// 鼠标画图功能
/// </summary>
public class Paint : MonoBehaviour
{
//画笔颜色
public Color paintColor = Color.red;
//画笔大小
public float paintSize = 0.1f;
//材质球
public Material lineMaterial;
private int indexPos;
private Vector3 lastPos;
private LineRenderer lineRenderer;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartPaint();
}
if (Input.GetMouseButton(0))
{
Painting();
}
}
//开始画
public void StartPaint()
{
GameObject go = new GameObject();
go.transform.SetParent(this.transform);
lineRenderer = go.AddComponent<LineRenderer>();
lineRenderer.positionCount = 0;
lineRenderer.material = lineMaterial;
lineRenderer.startWidth = paintSize;
lineRenderer.endWidth = paintSize;
lineRenderer.startColor = paintColor;
lineRenderer.endColor = paintColor;
lineRenderer.numCornerVertices = 50;
lineRenderer.numCapVertices = 50;
lastPos = Vector3.zero;
indexPos = 0;
}
//正在画
public void Painting()
{
Vector3 position = GetPaintPos();
if (lastPos != position)
{
indexPos++;
lineRenderer.positionCount = indexPos;
lineRenderer.SetPosition(indexPos - 1, position);
}
lastPos = position;
}
//获取绘画坐标
public Vector3 GetPaintPos()
{
return Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15));
}
}