using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Mspaint : MonoBehaviour
{
public Color color = Color.red; //画笔的颜色
public float size = 0.1f; //画笔粗细
public Material material; //材质球
private LineRenderer currentLine;//当前的LineRenderer组件
private List<Vector3> pos = new List<Vector3>(); //记录鼠标移动的位置点,赋值给LineRenderer组件使用
private Vector3 lastMousePos = Vector3.zero;//记录上次鼠标的点,防止在鼠标不移动的情况下还继续向pos 加入坐标
private int lineLayer = 1; //每条线段的层级,控制线段覆盖在上一条线段上
void Start()
{
//选择画笔颜色的按钮
Toggle[] colorGroup=this.transform.Find("ColorGroup").GetComponentsInChildren<Toggle>();
//选择画笔粗细的按钮
Toggle[] sizeGroup = this.transform.Find("SizeGroup").GetComponentsInChildren<Toggle>();
for (int i = 0; i < colorGroup.Length; i++)
{
colorGroup[i].onValueChanged.AddListener(OnColorChanged);
}
for (int i = 0; i < sizeGroup.Length; i++)
{
sizeGroup[i].onValueChanged.AddListener(OnSizeChanged);
}
}
private void OnColorChanged(bool ison)//改变画笔颜色的方法
{
GameObject game=EventSystem.current.currentSelectedGameObject;
if (!ison)
{
return;
}
switch (game.name)
{
case "Red":
color = Color.red;
break;
case "Blue":
color = Color.blue;
break;
case "Green":
color = Color.green;
break;
}
Debug.Log("当前笔刷颜色: " + color);
}
private void OnSizeChanged(bool ison)//改变画笔粗细的方法
{
GameObject game = EventSystem.current.currentSelectedGameObject;
if (!ison)
{
return;
}
switch (game.name)
{
case ".1":
size = 0.1f;
break;
case ".2":
size = 0.2f;
break;
case ".4":
size = 0.4f;
break;
}
Debug.Log("当前笔刷大小: "+size);
}
private Vector3 GetMousePoint()//获取鼠标射线碰撞点,用来确认线段点的位置
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit ;
if (Physics.Raycast(ray,out hit))
{
if (hit.collider!=null)
{
return hit.point;
}
}
return Vector3.zero;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))//鼠标按下
{
Vector3 startPos = GetMousePoint();
lastMousePos = startPos; //记录上次的鼠标点
GameObject go = new GameObject(); //每个线段都得克隆一个GameObject 且添加LineRenderer
go.transform.SetParent(this.transform);
currentLine = go.gameObject.AddComponent<LineRenderer>();
currentLine.material = material; //设置材质
currentLine.startWidth = size; //设置最开始的画笔大小
currentLine.endWidth = size; //设置最后一笔的画笔大小
currentLine.startColor = color; //设置最开始颜色
currentLine.endColor = color; //设置最后一笔的画笔颜色
currentLine.numCornerVertices = 5; //设置画笔的线段点 ,数越大代表线段越圆润
currentLine.numCapVertices = 5; //同上
currentLine.sortingOrder = lineLayer;//设置线段的显示层级,保证这条线段的显示在上一条线段之上
currentLine.SetPositions(pos.ToArray());//将鼠标位置数组赋值给线段
lineLayer++; //层级自加
pos.Clear(); //清空集合
pos.Add(startPos); //加入起始位置
}
else if (Input.GetMouseButton(0))//持续按着鼠标
{
Vector3 position = GetMousePoint();
if (Vector3.Distance(lastMousePos,position)>0.05f)//保证鼠标点开始移动,否则不加入鼠标位置点
{
lastMousePos = position; //记录上次位置
position.z -= 0.01f; //防止被画板挡住
pos.Add(position); //加入鼠标位置点
currentLine.positionCount = pos.Count;//因为是数组,所以需先设置长度,否则只会显示两个点
currentLine.SetPositions(pos.ToArray());//将鼠标位置数组赋值给线段
}
}
else if (Input.GetMouseButtonUp(0))//鼠标抬起,全部重置
{
pos.Clear();
currentLine = null;
lastMousePos = Vector3.zero;
}
}
}
1.材质球 2.线段的点的数组 3.画笔的颜色,可以设置渐变色 4.线段的顶点设置 5.线段的显示层级设置
材质球的设置
代码挂这