最近工程需要用到一个多边形用来查看角色属性,于是就研究了下Mesh用网格做了一个。遗憾的的 UGUI 渲染不了 3D 物体,然后又用了一段时间研究了下UGUI的网格绘制。

不过终于还是完成了,虽然有些瑕疵…… 好吧 有很大的问题 UV 需要自己计算。(如果有朋友精通这一块,希望能帮忙改进一下)

下边是5.2以下版本使用 的

5.2(包括)以上的 请点击这个连接

 在Unity中一个Mesh使用3个顶点就能画出来,但是UGUI需要使用四个顶点才行,所以在画一些特殊模型的时候就会产生一些废点(对图形没影响但是必须存在)。

下面要开始了。额……再等等,先让我借用几张图

Ok 先上使用教

先添加个这个

然后在这下边创建个空物体

然后添加这个脚本

然后就可以编辑这个脚本的属性 实现不同的多边形啦

使用polygon脚本 可以实现很多种形状 比如这些:

注意:上边这些都是UGUI,并不是模型网格;

5.2的UGUIAPI改动比较大所以不能想想兼容,我是在原作者的代码基础上改的。(希望原作者不会介意……额 我还是发个邮件说一下吧!!!!!!)

使用步骤和上边一样,这两个具有相同的问题就是UV的贴的时候没有贴好(原谅我这个数学渣渣),在原基础上增加了顶点网格的设置,其余的没有更改。

5.2(包涵)以上版本的 在上边的连接可以下载到 ,下边是5.2以下版本的。

 

//*********************************************************************
//
// ScriptName : UIPolygonPast
//
// 筱程
//
//********************************************************************* using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class UIPolygonPast : MaskableGraphic
{ List<Vector2> pointPosition = new List<Vector2>();
List<Vector2> Vertex = new List<Vector2>();
List<Vector2> UV = new List<Vector2>();
// 是否使用顶点画 默认是不使用
// 使用需要用户自己传入顶点位置
// 否则会根据顶点数目 以零点位置自动计算圆形顶点位置
[SerializeField]
Texture m_Textrue; public bool usingVertex; // 是否中空
public bool fill = true; // 顶点数目
[Range(, )]
public int sides = ;
// 选择角度
[Range(, )]
public float Rotation = ;
// 边缘厚度
[Range(, )]
public float Thickness = ;
// 顶点距离
[Range(, )]
public float[] VerticesDistances = new float[]; private float size = 1f;
public override Texture mainTexture {
get {
return m_Textrue == null ? s_WhiteTexture : m_Textrue;
}
}
public Texture texture {
get {
return m_Textrue;
}
set {
if (m_Textrue == value)
return;
m_Textrue = value;
SetVerticesDirty();
SetMaterialDirty();
}
}
/// <summary>
/// 设置图片顶点
/// </summary>
/// <param name="_Vertex"></param>
public void DrwaPolyonVertex(List<Vector2> _Vertex) {
DrwaPolyonVertex(_Vertex, null);
}
/// <summary>
/// 设置图片顶点
/// </summary>
/// <param name="_Vertex">顶点</param>
/// /// <param name="_UV">UI</param>
public void DrwaPolyonVertex(List<Vector2> _Vertex,List<Vector2> _UV) {
Vertex = _Vertex;
UV = _UV;
usingVertex = true;
}
/// <summary>
/// 设置顶点数目
/// </summary>
/// <param name="_sides"></param>
public void DrwaPolygon(int _sides) {
DrwaPolygon(_sides, null);
}
/// <summary>
/// 设置顶点数目
/// </summary>
/// <param name="_sides"></param>
/// <param name="_VerticesDistances">顶点距离信息</param>
public void DrwaPolygon(int _sides, float[] _VerticesDistances) {
DrwaPolygon(_sides, _VerticesDistances, );
}
/// <summary>
/// 设置顶点数目
/// </summary>
/// <param name="_sides"></param>
/// <param name="_VerTicesDstances">顶点距离信息</param>
/// <param name="_Rotation">顶点旋转</param>
public void DrwaPolygon(int _sides, float[] _VerTicesDstances,float _Rotation) {
sides = _sides;
if (_VerTicesDstances != null)
VerticesDistances = _VerTicesDstances;
Rotation = _Rotation;
usingVertex = false;
}
private Vector2 CalculatedPosition(Vector2 p1, Vector2 p2, Rect rect) {
p1.x *= rect.width;
p1.y *= rect.height;
return p1;
} // UI网格都是四边形,所以需要四个顶点
// 但是我们只需要三个顶点,所以就需要添加一个废点
// 否则不能组成网格
// 最后所有点数的总和应该是4的倍数
protected override void OnFillVBO(List<UIVertex> vbo) {
vbo.Clear(); if (usingVertex) {
VerterxDrwa(ref vbo);
} else {
VerterxNumberDrwa(ref vbo);
}
}
// 用顶点画
private void VerterxDrwa(ref List<UIVertex> vbo) {
Vector2 pos1 = Vector2.zero;
Vector2 pos2 = Vector2.zero;
Vector2 pos3 = Vector2.zero;
Vector2 pos4 = Vector2.zero;
if (Vertex.Count == ) {
pointPosition.Clear();
pointPosition.Add(new Vector2(, -) / 2f);
pointPosition.Add(new Vector2(-, -0.5f) / 2f); pointPosition.Add(new Vector2(-, 0.5f) / 2f);
pointPosition.Add(new Vector2(, ) / 2f);
pointPosition.Add(new Vector2(, 0.5f) / 2f);
pointPosition.Add(new Vector2(, -0.5f) / 2f);
} else {
pointPosition.Clear();
pointPosition = Vertex;
} for (int i = ; i < pointPosition.Count; i++) {
Vector2 pos = pointPosition[i];
pointPosition[i] = CalculatedPosition(pos, rectTransform.pivot, rectTransform.rect);
}
int count = pointPosition.Count;
Vector2 uv1 = new Vector2(, );
Vector2 uv2 = new Vector2(, );
Vector2 uv3 = new Vector2(, );
Vector2 uv4 = new Vector2(, );
for (int i = ; i < count - ; i++) { pos1 = pointPosition[i];
pos2 = pointPosition[i] + ( pointPosition[i] - Vector2.zero ) * -0.2f;
pos3 = Vector2.zero;
pos4 = pointPosition[i + ];
if (UV != null && count==UV.Count) {
uv3 = new Vector2(0.5f, 0.5f);
uv1 = UV[i];
uv4 = UV[i + ];
uv2 = uv1 + ( uv1 - uv3 ) * -0.2f;
}
AddUIVerterx(ref vbo, new Vector2[] { pos1, pos2, pos3, pos4 }, new Vector2[] { uv1, uv2, uv3, uv4 });
} pos1 = pointPosition[count - ];
pos2 = pointPosition[count - ] + ( pointPosition[count - ] - Vector2.zero ) * -0.2f;
pos3 = Vector2.zero;
pos4 = pointPosition[];
if (UV != null && count == UV.Count) {
uv1 = UV[count - ];
uv4 = UV[];
uv2 = uv1 + ( uv1 - uv3 ) * -0.2f;
}
AddUIVerterx(ref vbo, new Vector2[] { pos1, pos2, pos3, pos4 }, new Vector2[] { uv1, uv2, uv3, uv4 });
}
// 用顶点数画多边形
private void VerterxNumberDrwa(ref List<UIVertex> vbo) { float degrees = 360f / sides; size = rectTransform.rect.width > rectTransform.rect.height ? rectTransform.rect.height : rectTransform.rect.width;
Thickness = Mathf.Clamp(Thickness, , size / ); if (VerticesDistances == null || VerticesDistances.Length != sides + ) {
VerticesDistances = new float[sides + ];
for (int i = ; i < sides; i++)
VerticesDistances[i] = ;
}
VerticesDistances[sides] = VerticesDistances[];
Vector2 pos1 ;
Vector2 pos2 ;
Vector2 pos3 ;
Vector2 pos4 ;
Vector2 lastPosx = Vector2.zero;
Vector2 lastPosy = Vector2.zero;
Vector2 uv1 = new Vector2(, );
Vector2 uv2 = new Vector2(, );
Vector2 uv3 = new Vector2(, );
Vector2 uv4 = new Vector2(, );
//Vector2 lastuv = new Vector2(0.5f, 0.5f);
for (int i = ; i <= sides; i++) { float outer = -rectTransform.pivot.x * size * VerticesDistances[i];
float inner = -rectTransform.pivot.x * size * VerticesDistances[i] + Thickness;
float rad = Mathf.Deg2Rad * ( i * degrees + Rotation );
float c = Mathf.Cos(rad);
float s = Mathf.Sin(rad);
pos1 = lastPosx;
pos2 = new Vector2(outer * c, outer * s); if (fill) {
pos3 = Vector2.zero;
pos4 = Vector2.zero;
} else {
pos3 = new Vector2(inner * c, inner * s);
pos4 = lastPosy;
}
#region
//int x = (int) (pos1.x < 0 ? pos1.x * -1 : pos1.x * 2);
//int y = (int)( pos1.y < 0 ? pos1.y * -1 : pos1.y * 2 ); //float uvx = 0;
//float uvy = 0;
// if (x != 0) {
// uvx =x / size;
// }
// if (y != 0) {
// uvy =y / size;
// } //uv2 = new Vector2(uvx, uvy);
//uv1 = lastuv;
//lastuv = uv2;
#endregion
lastPosx = pos2;
lastPosy = pos3;
AddUIVerterx(ref vbo, new Vector2[] { pos1, pos2, pos3, pos4 }, new Vector2[] { uv1, uv2, uv3, uv4 });
}
}
private void AddUIVerterx(ref List<UIVertex> vbo, Vector2[] pos, Vector2[] uv = null) {
UIVertex vert = UIVertex.simpleVert;
for (int i = ; i < pos.Length; i++) {
vert.position = pos[i];
vert.color = color;
if (uv != null && uv.Length == pos.Length)
vert.uv0 = uv[i];
vbo.Add(vert);
}
}
}

Unity 绘制多边形的更多相关文章

  1. 用线框模式绘制多边形 glPolygonMode

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_TRIANGLES);//开始以g_ViewMode模式绘制 glColor3ub(182. ...

  2. [WebGL入门]十四,绘制多边形

    注意:文章翻译http://wgld.org/.原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外,鄙人webgl研究还不够深入.一些专业词语,假设翻译有误,欢迎大家 ...

  3. canvas绘制多边形

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. leaflet简单例子,绘制多边形

    var crs = L.CRS.EPSG900913; var map = L.map('map', { crs: crs, width: '100%', height: '100%', maxZoo ...

  5. 【Silverlight】Bing Maps学习系列(五):绘制多边形(Polygon)图形(转)

    [Silverlight]Bing Maps学习系列(五):绘制多边形(Polygon)图形 Bing Maps Silverlight Control支持用户自定义绘制多边形(Polygon)图形, ...

  6. 浅谈使用canvas绘制多边形

    本文主要使用坐标轴的使用来绘制多边形,点位则都是在y轴上寻找,这种方法能够更好的理解图形与修改. //id为html里canvas标签的属性id: //x,y为坐标轴的起始位置,因为canvas默认坐 ...

  7. JS实现鼠标点击爱心&绘制多边形&每日一言功能

    本篇文章主要介绍我的个人博客 程序猿刘川枫 中页面使用的美化功能(基于JS实现): 1.鼠标点击出现不同颜色爱心特效 2.页面浮动多边形跟随鼠标移动 3.每日一言功能 1.鼠标点击出现爱心特效 经常在 ...

  8. Unity动态构建mesh绘制多边形算法流程分析和实践

    前言 先说一下,写这篇博文的动机,原文的博主代码写的十分潇洒,以至于代码说明和注释都没有,最近恰逢看到,所以以此博文来分析其中的算法和流程 参考博文:https://blog.csdn.net/lin ...

  9. unity 绘制三角形

    哎 该学的还是要学 参考:http://www.narkii.com/club/thread-369573-1.html unity 顶点绘制三角形 脚本绘制; 其实filter和render就是进行 ...

随机推荐

  1. How to delete the icons of Win7 desktop shortcuts

    1. Copy the following bat code in txt type file, 2. save it as file extension type bat, run it as ad ...

  2. VC内存溢出一例 –- 调用约定不一致 (_CRT_DEBUGGER_HOOK(_CRT_DEBUGGER_GSFAILURE)

    VC (_CRT_DEBUGGER_HOOK(_CRT_DEBUGGER_GSFAILURE) 问题记录 VC内存溢出一例 –- 调用约定不一致 (_CRT_DEBUGGER_HOOK(_CRT_DE ...

  3. C#调用C++DLL传递结构体数组的终极解决方案

    在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...

  4. Chapter 13. Miscellaneous PerlTk Methods PerlTk 方法杂项:

    Chapter 13. Miscellaneous PerlTk Methods PerlTk 方法杂项: 到目前为止,这本书的大部分章节 集中在特定的几个部件, 这个章节覆盖了方法和子程序 可以被任 ...

  5. 学习笔记之--MySQL图形界面软件Navicat Premium的安装

    最近因项目开发需要,搁置已久的MySQL再次用到.由于以前都是使用命令行进行操作的,没有图形界面.经同学介绍,安装了一个MySQL的图形界面软件.各种数据库的操作也变得直观方便了很多.现在记录下来,一 ...

  6. 链表k个节点反向

    问题: 以k个元素为一组,反转单向链表.比如: 输入: 1->2->3->4->5->6->7->8->null and k = 3 输出:3-> ...

  7. 【网络协议】TCP的拥塞控制机制

    前言 计算机网络中的带宽.交换节点中的缓存和处理机等,都是网络的资源,在某段时间内,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就要变坏,这样的情况就叫做拥塞. 所谓拥塞控制,就 ...

  8. 性能强悍的开源关系数据库PostgreSQL

    性能强悍的开源关系数据库PostgreSQL

  9. PropertyGrid—默认属性,默认事件,属性默认值

    零.引言 PropertyGrid显示一个对象的属性和事件时,可以设置其默认属性和事件,也就是当你选中对象时,propertyGrid中焦点在哪一个属性或事件上.为对象的属性提供默认值,使Proper ...

  10. MSMQ是什么?

    MSMQ(MicroSoft Message Queue,微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一位 ...