当前位置: 首页 > news >正文

unity 画线写字

效果

1.界面设置

2.涉及两个脚本UIDraw.cs和UIDrawLine.cs

UIDraw.cs

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class UIDraw : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler , IPointerDownHandler, IPointerUpHandler
{public Color DrawColor = Color.red;[Range(3,10)]public int DrawWidth = 3;public Color BgColor = new Color(0, 0, 0, 0);Vector2 lastPosition;public UIDrawLine UIDrawLine;List<Vector2> Vertics = new List<Vector2>();List<List<Vector2>> CachVertics = new List<List<Vector2>>();void Awake(){}public RectTransform Drawer;void Update(){if (canDraw){Drawer.position = Input.mousePosition;DrawPoint(Drawer.anchoredPosition);#if UNITY_ANDROID||UNITY_IPHONE #endif}}public void OnPointerDown(PointerEventData eventData){canDraw = true;}public void OnPointerUp(PointerEventData eventData){canDraw = false;//添加结尾lastPosition = Vector2.zero;AddPoint(lastPosition);//添加到缓存AddToCach();}bool canDraw;public void OnPointerEnter(PointerEventData eventData){}public void OnPointerExit(PointerEventData eventData)  {lastPosition = Vector2.zero;AddPoint(lastPosition);}public void DrawPoint(Vector2 point){if (lastPosition == Vector2.zero){lastPosition = point;}UpdateColor(lastPosition, point, DrawWidth, DrawColor);}public void UpdateColor(Vector2 start_point, Vector2 end_point, int width, Color color){float distance = Vector2.Distance(start_point, end_point);if (distance < 0.1f) return;AddPoint(end_point) ;lastPosition = end_point;}void AddPoint(Vector2 v){Vertics.Add(v);UIDrawLine.Vertices.Add(v);UIDrawLine.SetAllDirty();}private void AddToCach(){List<Vector2> list = new List<Vector2>();list.AddRange(Vertics);CachVertics.Add(list);Vertics.Clear();}public void UnDo(){if (CachVertics.Count == 0) return;CachVertics.RemoveAt(CachVertics.Count-1);UIDrawLine.Vertices.Clear();for (int i = 0; i < CachVertics.Count; i++){UIDrawLine.Vertices.AddRange(CachVertics[i]);}UIDrawLine.SetAllDirty();}public void Send(){//UIDrawLine.Vertices;Clear();//gameObject.SetActive(false);}internal void Clear(){UIDrawLine.Vertices.Clear();UIDrawLine.SetAllDirty();Vertics.Clear();CachVertics.Clear();}void OnDisable() {Clear();}}

UIDrawLine.cs

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;[AddComponentMenu("UI/Extensions/UI Line")]
public class UIDrawLine : MaskableGraphic
{[SerializeField]Texture m_Texture;public float thickness = 5;public List<Vector2> Vertices=new List<Vector2> ();private float size = 0;public override Texture mainTexture{get{return m_Texture == null ? s_WhiteTexture : m_Texture;}}public Texture texture{get{return m_Texture;}set{if (m_Texture == value) return;m_Texture = value;SetVerticesDirty();SetMaterialDirty();}}protected override void OnEnable(){Vertices.Clear();}void Update(){}protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs){UIVertex[] vbo = new UIVertex[4];for (int i = 0; i < vertices.Length; i++){var vert = UIVertex.simpleVert;vert.color = color;vert.position = vertices[i];vert.uv0 = uvs[i];vbo[i] = vert;}return vbo;}//生成方格的四个点protected override void OnPopulateMesh(VertexHelper vh){vh.Clear();Vector2 uv0 = new Vector2(0, 1);Vector2 uv1 = new Vector2(1, 1);Vector2 uv2 = new Vector2(1, 0);Vector2 uv3 = new Vector2(0, 0);Vector2 lastPos0 = Vector2.zero;Vector2 lastPos1 = Vector2.zero;Vector2 lastPos2 = Vector2.zero;Vector2 lastPos3 = Vector2.zero;for (int i = 1; i < Vertices.Count; i++){//当有0是说明是结束点,开始下个画线if (Vertices[i - 1]==Vector2.zero || Vertices[i]== Vector2.zero){lastPos0 = Vector2.zero;continue;}Vector2 v = Rotate90(Vertices[i] - Vertices[i - 1]).normalized;Vector2 pos0 = Vertices[i - 1]- v * thickness * 0.5f;Vector2 pos1 = Vertices[i]- v * thickness * 0.5f;Vector2 pos2 = pos1 + v * thickness;Vector2 pos3 = pos0 + v * thickness;vh.AddUIVertexQuad(SetVbo(new[] { pos0, pos1, pos2, pos3 }, new[] { uv0, uv1, uv2, uv3 }));//角if (lastPos0!=Vector2.zero){Vector2 p0 = pos0;Vector2 p1 = lastPos1;Vector2 p2 = pos3;Vector2 p3 = lastPos2;vh.AddUIVertexQuad(SetVbo(new[] { p0, p1, p2, p3 }, new[] { uv0, uv1, uv2, uv3 }));}lastPos0 = pos0;lastPos1 = pos1;lastPos2 = pos2;lastPos3 = pos3;}}private Vector2 Rotate90(Vector2 p){return new Vector2(p.y, -p.x);}}

http://www.lryc.cn/news/424544.html

相关文章:

  • GitHub的详细介绍
  • 【鸿蒙学习】HarmonyOS应用开发者基础 - 构建更加丰富的页面之Tabs(三)
  • Detectron2 安装指南
  • 亚马逊 Linux mysql5.7 安装纪录
  • ZLMediaKit编译webrtc
  • KEEPALIVED高可用集群最详解
  • 【大模型】llama-factory基础学习
  • 【Java】如何使用jdbc连接并操作MySQL,一文读懂不迷路,小白也能轻松学会
  • 新手学习VR全景需要知道的几个问题
  • 上海知名泌尿外科专家常态化坐诊黄山新晨医院,让前列腺癌看得更早、更准!
  • Elasticsearch: 非结构化的数据搜索
  • 44 个 React 前端面试问题
  • LLMs之Framework:Hugging Face Accelerate后端框架之FSDP和DeepSpeed的对比与分析
  • HarmonyOS应用开发学习-ArkTs声明式UI描述
  • Redis20-通信协议
  • Unity Shader变体优化与故障排除技巧
  • 数据结构——时间复杂度和空间复杂度
  • (echarts) 饼图设置滚动图例
  • Java spring SSM框架--mybatis
  • Python知识点:如何使用Arduino与Python进行物联网项目
  • 论文复现_从 CONAN 中收集 TPL 数据集
  • 使用Docker将Java项目打包并部署到CentOS服务器的详细教程。
  • 嘉立创eda布线宽度
  • 硬件面试经典 100 题(31~50 题)
  • 5G:下一代无线通信技术的全面解析
  • 关于refresh_token
  • Linux网络:基于OS的网络架构
  • UEC++学习(十六)变量添加中文注释、ui设置中文文本
  • Redis延迟双删
  • WO Mic 手机变身免费麦克风