我这里用到了Qframeworke插件。
如果不用的话你可以直接删掉相关的代码就可以了。
废话不多说,先上代码!
附上资源链接:我是想免费分享的,但是那个什么积分是自动的,我没法设置多少积分
https://download.csdn.net/download/qq_42489774/11890690
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using QF.Res;
///
/// 存储路径的父物体
///
public class Mspaint : MonoBehaviour
{
[SerializeField]
private int state = 0;
//画笔颜色
private Color paintColor = Color.red;
[HideInInspector]
//画笔大小
private float paintSize = 0.05f;
//LineRenderer 组件
private LineRenderer curretnLine;
//画笔材质
public Material lineMaterial;
//存储画线节点
private List positions = new List();
private bool isMouseDown = false;
//记录上一次节点位置
private Vector3 lastMousePosition = Vector3.zero;
//圆圈的半径
public Slider slider;
public GameObject CanvasX;
//装line盒子
public Transform ChildrenBox;
//是否可以执行画画
bool IsPaint = true;
//划线,直线/圆圈等等模式
private int Line_Mode = 0;
public Dropdown dropdown;
//圆圈
Vector3 v; //起点,Vector2是2D,当然也可以换Vector3
Vector3 origin;
float R; //半径
int positionCount; //完成一个圆的总点数,
float angle; //转角,三个点形成的两段线之间的夹角
Quaternion q; //Quaternion四元数
//圆圈
public bool IsCircle = false;
public bool IsLine = false;
[SerializeField]
public RawImage RayImage;
private Texture2D Pen;
//private Sprite goods;
ResLoader loader = new ResLoader();
private void Awake()
{
//Value可以不用传过去,因为这个值可以从item中获取;
slider.onValueChanged.AddListener((float value) => slider.transform.GetChild(0).GetComponent().text=value.ToString());
dropdown.onValueChanged.AddListener((int value) => Line_Mode = value);
Pen = loader.LoadSync("Pen");
// goods = Sprite.Create(Pen, new Rect(0, 0, Pen.width, Pen.height), new Vector2(0.5f, 0.5f));
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.F3))
{
IsPaint = !IsPaint;
ChildrenBox.gameObject.SetActive(!IsPaint);
CanvasX.gameObject.SetActive(!IsPaint);
RayImage.gameObject.SetActive(!IsPaint);
if (!IsPaint)
{
Cursor.SetCursor(Pen, new Vector2(0f,50f),CursorMode.ForceSoftware);
}
else
{
Cursor.SetCursor(null,new Vector2(0.5f,0.5f),CursorMode.ForceSoftware);
}
}
if (Input.GetKeyUp(KeyCode.Escape))
{
IsPaint = true;//禁止绘画
ChildrenBox.gameObject.SetActive(false);
CanvasX.gameObject.SetActive(false);
}
if (!IsPaint)
{
//撤销功能
if (Input.GetKeyDown(KeyCode.F2))
{
if (state > 0)
{
Destroy(ChildrenBox.Find("Line_" + state).gameObject);
state--;
}
}
if (Input.GetMouseButtonDown(0) && Line_Mode == 0)//自由画线
{
IsLine = false;
state++;
//新建一个空物体
GameObject go = new GameObject("Line_" + state);
go.transform.SetParent(ChildrenBox);
//给空物体添加LineRenderer组件
curretnLine = go.AddComponent();
//设置材质
curretnLine.material = lineMaterial;
//画笔大小
curretnLine.startWidth = paintSize;
curretnLine.endWidth = paintSize;
//画笔颜色
curretnLine.startColor = paintColor;
curretnLine.endColor = paintColor;
//画笔圆滑度
curretnLine.numCornerVertices = 5;
curretnLine.numCapVertices = 5;
//返回射线和平面交点位置
Vector3 position = GetMousePoint();
//添加画线路劲节点(Vector3)
AddPosition(position);
isMouseDown = true;
}
else if (Input.GetMouseButtonDown(0) && Line_Mode == 1)//
{
IsLine = false;
state++;
//新建一个空物体
GameObject go = new GameObject("Line_" + state);
go.transform.SetParent(ChildrenBox);
//给空物体添加LineRenderer组件
curretnLine = go.AddComponent();
//设置材质
curretnLine.material = lineMaterial;
//画笔大小
curretnLine.startWidth = paintSize;
curretnLine.endWidth = paintSize;
//画笔颜色
curretnLine.startColor = paintColor;
curretnLine.endColor = paintColor;
//画笔圆滑度
curretnLine.numCornerVertices = 5;
curretnLine.numCapVertices = 5;
v = GetMousePoint();
// R = 6;
positionCount = 180;
angle = 360f / (positionCount - 1);
//curretnLine = curretnLine.GetComponent();
curretnLine.positionCount = positionCount;
curretnLine.SetPosition(0, v);
curretnLine.SetPosition(1, v);
IsCircle = true;
}
if (IsCircle&& Line_Mode == 1)
{
curretnLine.SetPosition(1, GetMousePoint());
R = Vector3.Distance(v, GetMousePoint())/2;
origin = (v + GetMousePoint()) / 2;
Debug.Log(R);
DrawCircle2();
}
if (Input.GetMouseButtonUp(0) && Line_Mode == 1)
{
positionCount = 180;
curretnLine.SetPosition(0, v);
curretnLine.SetPosition(1, GetMousePoint());
origin =( v + GetMousePoint())/2;
IsCircle = false;
DrawCircle();
//isMouseDown = true;
//Vector3 position = GetMousePoint();
//添加画线路劲节点(Vector3)
//AddPosition(position);
}
if (Input.GetMouseButtonDown(0) && Line_Mode == 2)
{
state++;
//新建一个空物体
GameObject go = new GameObject("Line_" + state);
go.transform.SetParent(ChildrenBox);
//给空物体添加LineRenderer组件
curretnLine = go.AddComponent();
//设置材质
curretnLine.material = lineMaterial;
//画笔大小
curretnLine.startWidth = paintSize;
curretnLine.endWidth = paintSize;
//画笔颜色
curretnLine.startColor = paintColor;
curretnLine.endColor = paintColor;
//画笔圆滑度
curretnLine.numCornerVertices = 5;
curretnLine.numCapVertices = 5;
curretnLine.positionCount = 2;//设置两点
Vector3 position = GetMousePoint();
//设置指示线的起点和终点
IsLine = true;
curretnLine.SetPosition(0, position);
}
if (IsLine&&Line_Mode == 2)
{
curretnLine.SetPosition(1, GetMousePoint());
}
if (Input.GetMouseButtonUp(0) && Line_Mode == 2)
{
curretnLine.positionCount = 2;//设置两点
IsLine = false;
Vector3 position = GetMousePoint();
curretnLine.SetPosition(1, position);
}
void DrawCircle()
{
if (Line_Mode == 1)
{
positionCount = 180;
}
else if (Line_Mode == 2)
{
positionCount = 2;
}
for (int i = 0; i < positionCount; i++)
{
if (i != 0)
{
q = Quaternion.Euler(q.eulerAngles.x, q.eulerAngles.y, q.eulerAngles.z + angle);
}
if (Line_Mode == 1)
{
Vector3 forwardPosition = (Vector3)origin + q * Vector3.down * R;//slider.value*10;
curretnLine.SetPosition(i, forwardPosition);
}
else if (Line_Mode == 2)
{
positionCount = 2;
Vector3 forwardPosition = (Vector3)origin + q * Vector3.down * R;//slider.value*10;
curretnLine.SetPosition(i, forwardPosition);
}
}
}
void DrawCircle2()
{
if (Line_Mode == 1)
{
positionCount = 180;
}
else if (Line_Mode == 2)
{
positionCount = 2;
}
for (int i = 0; i < positionCount; i++)
{
if (i != 0)
{
q = Quaternion.Euler(q.eulerAngles.x, q.eulerAngles.y, q.eulerAngles.z + angle);
}
Vector3 forwardPosition = (Vector3)origin + q * Vector3.down * R;//slider.value*10;
curretnLine.SetPosition(i, forwardPosition);
}
}
//记录鼠标滑过的路劲节点
if (isMouseDown)
{
//返回射线和平面交点位置
Vector3 position = GetMousePoint();
if (Vector3.Distance(position, lastMousePosition) > 0.1f)
{
//添加画线路径节点(Vector3)
AddPosition(position);
}
}
//鼠标抬起时
if (Input.GetMouseButtonUp(0))
{
curretnLine = null;
//清空上一次的画线节点 list
positions.Clear();
isMouseDown = false;
}
}
}
//添加画线路劲节点(Vector3)
void AddPosition(Vector3 position)
{
//让画线前置画布0.1距离防止部分画线不显示
position.z -= 0.1f;
positions.Add(position);
//
curretnLine.positionCount = positions.Count;
curretnLine.SetPositions(positions.ToArray());
lastMousePosition = position;
}
//返回射线和平面交点位置
Vector3 GetMousePoint()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
//返回射线和平面交点位置
return hit.point;
}
return Vector3.zero;
}
#region
//画布上的toggle按钮事件
//切换为红色
public void OnRedColorChanged(bool isOn)
{
if (isOn)
{
paintColor = Color.red;
}
}
//切换为黄色
public void OnYellowColorChanged(bool isOn)
{
if (isOn)
{
paintColor = Color.yellow;
}
}
//切换为白色
public void OnWhiteColorChanged(bool isOn)
{
if (isOn)
{
paintColor = Color.white;
}
}
//切换为黑色
public void OnBlackColorChanged(bool isOn)
{
if (isOn)
{
paintColor = Color.black;
}
}
//设置画线粗细 0.1
public void OnPoint1Changed(bool isOn)
{
if (isOn)
{
paintSize = 0.05f;
}
}
//设置画线粗细 0.2
public void OnPoint2Changed(bool isOn)
{
if (isOn)
{
paintSize = 0.1f;
}
}
//设置画线粗细 0.4
public void OnPoint4Changed(bool isOn)
{
if (isOn)
{
paintSize = 0.2f;
}
}
//清空画布
public void OnClearPanelButton()
{
state = 0;
if (transform.childCount > 0)
{
int lineCount = ChildrenBox.childCount;
for (int i = 0; i < ChildrenBox.childCount; i++)
{
Destroy(ChildrenBox.GetChild(i).gameObject);
}
}
}
#endregion
}