基于UGUI的Unity画线工具

最近项目里需要做一个画线的小游戏,LineRenderer不是很好用,自己撸了一个小工具,效果如下

下面上代码

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using FMCShape;public class MyLineDrawer : Image{public List<Vector2> pointer = new List<Vector2>();public float radio = 30;private int headPointCount = 7;//头部顶点数量private Vector2 offset;protected override void Awake(){base.Awake();offset = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);}protected override void OnPopulateMesh(VertexHelper toFill){toFill.Clear();if (pointer.Count >= 2){for (int i = 1; i < pointer.Count; i++){Draw(toFill, pointer[i - 1], pointer[i]);}}}public void AddPointer(Vector2 pointer){this.pointer.Add(pointer);ReDraw();}public void ReDraw(){SetVerticesDirty();}void Draw(VertexHelper vh, Vector2 start, Vector2 end){Vector2 to = end - start;Vector2 nor_to = to.normalized * radio + start;Vector2 up = MathHepler.RotateVector2(90, nor_to, start);Vector2 down = MathHepler.RotateVector2(-90, nor_to, start);Vector2 up_end = up + to;Vector2 down_end = down + to;//添加直线AddQuad(vh, up, down, up_end, down_end);List<Vector2> tempPointer = ListPool<Vector2>.Get();//添加左边头部float angel = 180 / (headPointCount + 1);tempPointer.Add(down);for (float i = -angel; i > -180; i -= angel){tempPointer.Add(MathHepler.RotateVector2(i, down, start));}tempPointer.Add(up);for (int i = 1; i < tempPointer.Count; i++){AddVert(vh, tempPointer[i - 1], tempPointer[i], start);}//添加右边头部tempPointer.Clear();tempPointer.Add(up_end);for (float i = -angel; i > -180; i -= angel){tempPointer.Add(MathHepler.RotateVector2(i, up_end, end));}tempPointer.Add(down_end);for (int i = 1; i < tempPointer.Count; i++){AddVert(vh, tempPointer[i - 1], tempPointer[i], end);}ListPool<Vector2>.Release(tempPointer);}void AddVert(VertexHelper vh, Vector2 pos1, Vector2 pos2, Vector2 pos3){AddVert(vh, CreateEmptyVertex(pos1), CreateEmptyVertex(pos2), CreateEmptyVertex(pos3));}void AddVert(VertexHelper vh, UIVertex v1, UIVertex v2, UIVertex v3){int index = vh.currentVertCount;vh.AddVert(v1);vh.AddVert(v2);vh.AddVert(v3);vh.AddTriangle(index, index + 1, index + 2);}void AddQuad(VertexHelper vh, Vector2 pos1, Vector2 pos2, Vector2 pos3, Vector2 pos4){AddQuad(vh, CreateEmptyVertex(pos1), CreateEmptyVertex(pos2), CreateEmptyVertex(pos3), CreateEmptyVertex(pos4));}void AddQuad(VertexHelper vh, UIVertex v1, UIVertex v2, UIVertex v3, UIVertex v4){int index = vh.currentVertCount;vh.AddVert(v1);vh.AddVert(v2);vh.AddVert(v3);vh.AddVert(v4);vh.AddTriangle(index, index + 1, index + 2);vh.AddTriangle(index + 2, index + 3, index + 1);}UIVertex CreateEmptyVertex(Vector2 pos){UIVertex v = new UIVertex();v.position = pos;//需要修改颜色,在这里改就可以了v.color = color;v.uv0 = Vector2.zero;return v;}
}

使用时当做一个image就可以,代码如下

    public MyLineDrawer line;private void Update(){if (Input.GetMouseButtonDown(0)){line.AddPointer(Input.mousePosition-new Vector3(540,960));}}

基于UGUI的Unity画线工具相关推荐

  1. Unity画线工具--LineRander

    新版的画线系统将颜色和材质渐变进行了整合,根据官方的文档,我们可以发现将这些整合到了Gradient这个类里面,这是一个专门用来处理渐变的类 private LineRenderer lr;void ...

  2. 【Unity】UI画线工具

    [Unity]UI画线工具 最近恰好遇到在图片上画线标记路径的需求,需要动态地画很多线,感觉做帧动画的话美术工作量有点大,所以做了一个功能比较简单的UI画线工具 UIPathwaySystem : h ...

  3. visio如何找到画线工具

    visio如何找到画线工具 在进入Visio界面的时候,左侧有绘制箭头.流程图等工具,但是有时候流程图需要绘制自己定义的线条,此时可以调用绘图工具. 在菜单栏右键空白地方,选择绘图选项: 得到绘图工具 ...

  4. 基于vue的svg画线_基于SVG的Vue图组件库

    基于vue的svg画线 图表 (diagram-vue) A vue component library of diagrams. Vue组件图库. View Demo 查看演示 Download S ...

  5. 通达信交易服务器修改,通达信画线交易价格修改,通达信画线工具详解

    Q3:通达信成本价的修改问题? 你指的是通达信个人理财参数?还是委托交易设置? 都可以改回来. Q4:通达信里如何把成交明细数量换成金额显示,有知道怎么设置的吗 在工具栏的工具中-系统设置-设置1-成 ...

  6. Unity画线(Vectrosity5.6.1插件)

    一个好的插件,会让你事半功倍,在Unity这个如此成熟的生态圈里,几乎常用的功能,都会有对应的插件帮助速简化开发工作,以最低的时间成本达到目的.接下来要介绍的是Unity的一款功能强大的画线插件:Ve ...

  7. Unity画线之GL

    上一篇中,SetPixel的方法,卡顿严重,暂未解决,又去看了原来的GL画线,自己画图思考了一下适配UI的问题,最终解决. 特此说明,GL画线功能,及Shader均为借鉴,自己做了优化. 程序代码如下 ...

  8. unity画线之模拟小球抛物线运动轨迹

    模拟小球的抛物线运动,如图所示: 这里有点像打台球游戏,模拟在发射之前模拟其轨迹. 划线用的是LineRenderer,不清楚的童鞋可以自行查阅咯.其实也很简单就是挂个LineRenderer脚本,设 ...

  9. [AHK]为通达信画线工具中的文字注释功能增加热键

    不想每次都用鼠标去寻找定位这个功能,想热键直达. 方法一:点击坐标法,问题是分辨率不同会导致失效. SendMessage,0x111,4104,0,,ahk_class TdxW_MainFrame ...

最新文章

  1. 你写的ML代码占多少内存?这件事很重要,但很多人还不懂
  2. python字典的实现原理_Python字典的实现原理
  3. C 一个非递减数组 下标从0到n 元素的取值范围为从0到n的整数 判断其中是否有重复元素
  4. 【错误记录】国际化报错 ( “xxx“ is not translated in “zh“ (Chinese) )
  5. 前端学习(3318):异步处理thunk
  6. RDD之四:Value型Transformation算子
  7. 学习Ruby的10条理由
  8. highcharts 折线图 和柱状图读取 json值
  9. fisco bcos transaction交易结构 源代码位置
  10. UG软件_NX1926中文版网盘下载链接+安装教程
  11. YYKit 源码探究
  12. 小程序 轮播图样式设置
  13. goahead 的认证和自定义登陆页面的cookie使用
  14. 多一份感动,多一份行动
  15. 符号代数方程求解,分析可视化 dsolve函数
  16. 如何阅读一本书 笔记
  17. mac终端命令(苹果终端命令)
  18. 可视化实例(三)Tableau基础绘图介绍——横向条形图、双轴折线图、直方图
  19. 西游记中出现的女神仙
  20. 计算机数值数据的编码,数字数据编码

热门文章

  1. ARMv8-A编程指导之电源管理(1)
  2. MTK开发之—开机动画log修改
  3. 计算机体系结构-体系结构基础与流水线原理
  4. 二手车买卖APP功能比较
  5. 早上起床后,如何快速调整心情?
  6. 与曾宪寿共探中医应用属性数学之路一(作者:赵致生)
  7. 数据链路层的三个基本问题
  8. 简易却不失华丽 [进销存管理](BCB+ACCESS)
  9. 武交武铁武城计算机类专业哪个学校好,重磅!我校入选国家教育部重要名单!快来支持武交院!...
  10. javascript 使用json 将js 数据转换成json