UnityGizmos绘制扇形
线框扇形是⽤线条拼起来的,实⼼的扇形的是提前⽣成好mesh,然后⽤drawmesh。
using UnityEngine;
public static class GizmosTools
{
/// <summary>
/// 绘制半圆
/// </summary>
public static void DrawWireSemicircle(Vector3 origin,Vector3 direction,float radius,int angle)
{
DrawWireSemicircle(origin,direction,radius,angle,Vector3.up);semicircle
}
public static void DrawWireSemicircle(Vector3 origin,Vector3 direction,float radius,int angle,Vector3 axis)    {
Vector3 leftdir = Quaternion.AngleAxis(-angle/2, axis)*direction;
Vector3 rightdir = Quaternion.AngleAxis(angle/2, axis)*direction;
Vector3 currentP = origin + leftdir * radius;
Vector3 oldP;
if (angle!=360)
{
Gizmos.DrawLine(origin,currentP);
}
for (int i = 0; i < angle/10; i++)
{
Vector3 dir= Quaternion.AngleAxis(10*i, axis)*leftdir;
oldP = currentP;
currentP=origin + dir * radius;
Gizmos.DrawLine(oldP,currentP);
}
oldP = currentP;
currentP=origin + rightdir * radius;
Gizmos.DrawLine(oldP,currentP);
if (angle!=360)
{
Gizmos.DrawLine(currentP,origin);
}
}
public static Mesh SemicircleMesh(float radius,int angle,Vector3 axis)
{
Vector3 leftdir = Quaternion.AngleAxis(-angle/2, axis)*Vector3.forward;
Vector3 rightdir = Quaternion.AngleAxis(angle/2, axis)*Vector3.forward;
int pcount = angle / 10;
int pcount = angle / 10;
//顶点
Vector3[] vertexs = new Vector3[3+pcount];
vertexs[0] = ;
int index = 1;
vertexs[index] = leftdir * radius;
index++;
for (int i = 0; i < pcount; i++)
{
Vector3 dir= Quaternion.AngleAxis(10*i, axis)*leftdir;            vertexs[index]= dir * radius;
index++;
}
vertexs[index] = rightdir * radius;
//三⾓⾯
int[] triangles=new int[3*(1+pcount)];
for (int i = 0; i < 1+pcount; i++)
{
triangles[3 * i] = 0;
triangles[3 * i + 1] = i+1;
triangles[3 * i + 2] = i+2;
}
Mesh mesh=new Mesh();
mesh.vertices = vertexs;
mesh.RecalculateNormals();
return mesh;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。