Unity 好用的Gizmos.DrawMesh

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshTools
{
    /// 圆形
    /// 
    /// 
    /// 
    /// 
    public static Mesh MeshRound(float radio, int pointAmount)
    {
        float eachAngle = 360f / pointAmount;
        List vertices = new List();
        for (int i = 0; i <= pointAmount; i++)
        {
            Vector3 pos = Quaternion.Euler(0f, eachAngle * i, 0f) * Vector3.forward * radio;
            vertices.Add(pos);
        }
        int[] triangles;
        Mesh mesh = new Mesh();

        int trangleAmount = vertices.Count - 2;
        triangles = new int[3 * trangleAmount];

        for (int i = 0; i < trangleAmount; i++)
        {
            triangles[3 * i] = 0;
            triangles[3 * i + 1] = i + 1;
            triangles[3 * i + 2] = i + 2;
        }
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        return mesh;
    }

    /// 
    /// 矩形
    /// 
    /// 
    /// 
    /// 
    /// 
    public static Mesh MeshRect(Transform target, float width, float height)
    {

        List vertices = new List();
        vertices.Add(new Vector3(0, 0, 0) - target.right * (width / 2));
        vertices.Add(new Vector3(0, 0, 0) - target.right * (width / 2) + target.forward * height);
        vertices.Add(new Vector3(0, 0, 0) + target.right * (width / 2) + target.forward * height);
        vertices.Add(new Vector3(0, 0, 0) + target.right * (width / 2));

        int[] triangles;
        Mesh mesh = new Mesh();

        int trangleAmount = vertices.Count - 2;
        triangles = new int[3 * trangleAmount];

        for (int i = 0; i < trangleAmount; i++)
        {
            triangles[3 * i] = 0;
            triangles[3 * i + 1] = i + 1;
            triangles[3 * i + 2] = i + 2;
        }

        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        return mesh;
    }

    /// 
    /// 矩形
    /// 
    /// 
    /// 
    /// 
    /// 
    public static Mesh MeshRect(Vector3 pos, float width, float height)
    {
        List vertices = new List();
        vertices.Add(new Vector3(width / 2, pos.y, height / 2));
        vertices.Add(new Vector3(width / 2, pos.y, -height / 2));
        vertices.Add(new Vector3(-width / 2, pos.y, -height / 2));
        vertices.Add(new Vector3(-width / 2, pos.y, height / 2));

        Mesh mesh = new Mesh();
        int[] triangles = new int[3 * 2];
        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 2;
        triangles[3] = 0;
        triangles[4] = 2;
        triangles[5] = 3;

        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        return mesh;
    }

    /// 
    /// 扇形
    /// 
    /// 
    /// 
    /// 
    /// 
    public static Mesh MeshFan(float angle, float maxRadio, float minRadio)
    {
        //因为vertices(顶点)的个数与triangles(索引三角形顶点数)必须匹配
        int vertices_count = 60 * 2 + 2;
        Vector3[] vertices = new Vector3[vertices_count];
        float angleRad = Mathf.Deg2Rad * angle;
        float angleCur = angleRad;
        float angledelta = angleRad / 60;
        for (int i = 0; i < vertices_count; i += 2)
        {
            float cosA = Mathf.Cos(angleCur);
            float sinA = Mathf.Sin(angleCur);

            vertices[i] = new Vector3(maxRadio * cosA, 0, maxRadio * sinA);
            vertices[i + 1] = new Vector3(minRadio * cosA, 0, minRadio * sinA);
            angleCur -= angledelta;
        }

        //triangles:
        int triangle_count = 60 * 6;
        int[] triangles = new int[triangle_count];
        for (int i = 0, vi = 0; i < triangle_count; i += 6, vi += 2)
        {
            triangles[i] = vi;
            triangles[i + 1] = vi + 3;
            triangles[i + 2] = vi + 1;
            triangles[i + 3] = vi + 2;
            triangles[i + 4] = vi + 3;
            triangles[i + 5] = vi;
        }

        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        return mesh;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum CheckRange
{
    Round, //圆形
    Fan, //扇形
    Rect //矩形
}

public class CheckPoint : MonoBehaviour
{
    //战斗区形状
    [Space(20)]
    public CheckRange CheckRange = CheckRange.Round;
    //圆形半径
    [Header("Round:")]
    public float RoundRadio = 0;
    //扇形参数
    [Header("Fan")]
    //角度
    public float FanEnagle = 0;
    //最小距离
    [Range(0, 1)]
    public float FanMinRange = 0;
    //最大剧情
    public float FanMaxRange = 0;
    //获取最小距离
    public float GetFanMinRange
    {
        get
        {
            return FanMaxRange * FanMinRange;
        }
    }

    //矩形范围
    [Header("Rect")]
    //长度
    public float RectWidth = 0;
    //宽度
    public float RectHeight = 0;
    //是否绘制Gizmos
    public bool IsGzimos = false;

    void OnDrawGizmos()
    {
        //if (!IsGzimos) return;
        DrawGizmos();
    }
    //绘制mesh
    void DrawGizmos()
    {
        switch (CheckRange)
        {
            case CheckRange.Round:
                Mesh round = MeshTools.MeshRound(RoundRadio, 60);
                Gizmos.color = new Color(1, 0, 0, 0.5f);
                Gizmos.DrawMesh(round, transform.position);
                break;
            case CheckRange.Fan:
                Mesh fan = MeshTools.MeshFan(FanEnagle, FanMaxRange, GetFanMinRange);
                Gizmos.color = new Color(1, 0, 0, 0.5f);
                Gizmos.DrawMesh(fan, transform.position);
                break;
            case CheckRange.Rect:
                Mesh rect = MeshTools.MeshRect(transform.position, RectWidth, RectHeight);
                Gizmos.color = new Color(1, 0, 0, 0.5f);
                Gizmos.DrawMesh(rect, transform.position);
                break;
        }
    }
}

你可能感兴趣的:(Unity 好用的Gizmos.DrawMesh)