84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.UI;
|
|
using Unity.VisualScripting;
|
|
using System.Collections.Generic;
|
|
|
|
[ExecuteInEditMode]
|
|
public class PolygonUIMesh : Image
|
|
{
|
|
public List<Transform> points = new List<Transform>();
|
|
|
|
private void OnGUI()
|
|
{
|
|
// 实时检测更新绘制 OnPopulateMesh 中 transform.child 位置
|
|
SetAllDirty();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据 transform.child 位置 绘制 Mesh
|
|
/// </summary>
|
|
/// <param name="vh"></param>
|
|
protected override void OnPopulateMesh(VertexHelper vh)
|
|
{
|
|
if (points.Count <= 2)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Color32 color32 = color;
|
|
vh.Clear();
|
|
|
|
GenerateSimpleSprite(vh, false);
|
|
}
|
|
|
|
void GenerateSimpleSprite(VertexHelper vh, bool lPreserveAspect)
|
|
{
|
|
//Vector4 v = GetDrawingDimensions(lPreserveAspect);
|
|
var uv = (sprite != null) ? UnityEngine.Sprites.DataUtility.GetOuterUV(sprite) : Vector4.zero;
|
|
|
|
var color32 = color;
|
|
vh.Clear();
|
|
//vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(uv.x, uv.y));
|
|
//vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(uv.x, uv.w));
|
|
//vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(uv.z, uv.w));
|
|
//vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(uv.z, uv.y));
|
|
|
|
//vh.AddTriangle(0, 1, 2);
|
|
//vh.AddTriangle(2, 3, 0);
|
|
|
|
// 几何图形的顶点,本例中根据子节点坐标确定顶点
|
|
vh.AddVert(points[0].localPosition, color32, new Vector2(uv.x, uv.y));
|
|
vh.AddVert(points[1].localPosition, color32, new Vector2(uv.x, uv.w));
|
|
vh.AddVert(points[2].localPosition, color32, new Vector2(uv.z, uv.w));
|
|
vh.AddVert(points[3].localPosition, color32, new Vector2(uv.z, uv.y));
|
|
|
|
vh.AddTriangle(0, 1, 2);
|
|
vh.AddTriangle(2, 3, 0);
|
|
|
|
//for (int i = 0; i < (transform.childCount - 2); i++)
|
|
//{
|
|
// // 几何图形中的三角形
|
|
// vh.AddTriangle(i + 1, i + 2, 0);
|
|
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 点的辅助绘制
|
|
/// </summary>
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (transform.childCount == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
{
|
|
Gizmos.DrawSphere(transform.GetChild(i).position, 2.55f);
|
|
Gizmos.DrawIcon(transform.GetChild(i).position, "numbers/numbers_" + i + ".PNG", false);
|
|
}
|
|
|
|
}
|
|
} |