1.父物体添加mask
02.
Uv
求出每一块的弧度
通过弧度可以求出角度 角度可以知道顶点信息 由顶点信息获取三角面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;
public class CircularImg :Image
{
/// <summary>
/// 圆形由无数个三角形拼凑而成 可以知道每一份的三角度
/// </summary>
private int segements;
protected override void OnPopulateMesh(VertexHelper vh)
{
segements = 1000;
//清空原有数据
vh.Clear();
//获取图片的宽和高
float width = rectTransform.rect.width;
float height= rectTransform.rect.height;
//获取UV信息
Vector4 uv=overrideSprite!=null?DataUtility.GetOuterUV(overrideSprite):Vector4.zero;
float uvWidth = uv.z - uv.x;
float uvHeight = uv.w - uv.y;
//获取UV的中心点
Vector2 uvCenter=new Vector2((uvWidth)*0.5f,(uvHeight)*0.5f);
//换算比例
Vector2 converRatio=new Vector2(uvWidth/width,uvHeight/height);
//求出三角形每一块的弧度 2π*(角度/360)
float radian= 2 * Mathf.PI / segements;
//通过弧度可以求出角度 角度可以知道顶点信息 由顶点信息可以绘制三角形
float radius = width *0.5f; //圆形的半径
//可以求出每一个三角形的宽高 获取顶点坐标
//获取圆心顶点的颜色和坐标
Vector2 posTermp=Vector2.zero;
UIVertex origin=new UIVertex();
origin.color = color;
origin.position = posTermp;
origin.uv0=new Vector2(posTermp.x*converRatio.x+uvCenter.x, posTermp.y * converRatio.y + uvCenter.y);
vh.AddVert(origin);
int vertexCount = segements + 1;
float curRadian = 0;
for (int i = 0; i < vertexCount; i++)
{
float x = Mathf.Cos(curRadian) * radius;
float y = Mathf.Sin(curRadian) * radius;
curRadian += radian;
UIVertex vertexTemp=new UIVertex();
vertexTemp.color = color;
posTermp=new Vector2(x,y);
vertexTemp.position = posTermp;
vertexTemp.uv0=new Vector2(posTermp.x * converRatio.x + uvCenter.x, posTermp.y * converRatio.y + uvCenter.y);
vh.AddVert(vertexTemp);
}
int id = 1;
for (int i = 0; i < segements; i++)
{
vh.AddTriangle(id,0,id+1);
id++;
}
}
}
03.精准点击