using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Script
{
class Axis: MaskableGraphic
{
/// <summary>
/// 点的像素大小
/// </summary>
[Range(0, 0.1f)]
public float PixelScal = 0.05f;
/// <summary>
/// 坐标轴原点
/// </summary>
public Vector3 Position_O = new Vector3(5, 5, 0);
/// <summary>
/// x轴终点坐标
/// </summary>
public Vector3 XEndPosion = new Vector3(50, 5, 0);
/// <summary>
/// Y轴终点坐标
/// </summary>
public Vector3 YEndPosion = new Vector3(5, 50, 0);
/// <summary>
/// x轴最大值
/// </summary>
public float XMax = 100;
/// <summary>
/// y轴最大值
/// </summary>
public float YMax = 100;
/// <summary>
/// x轴刻度数
/// </summary>
[Range(5, 20)]
public int XNum = 18;
/// <summary>
/// y轴刻度数
/// </summary>
[Range(5, 20)]
public int YNum = 10;
/// <summary>
/// x轴名称
/// </summary>
public string XText;
/// <summary>
/// y轴名称
/// </summary>
public string YText;
/// <summary>
/// x轴单位
/// </summary>
public string XUnit;
/// <summary>
/// y轴单位
/// </summary>
public string YUnit;
/// <summary>
/// 画布width
/// </summary>
public float CanvasXScal = 50;
/// <summary>
/// 画布Height;
/// </summary>
public float CanvasYScal = 50;
/// <summary>
/// 刻度颜色
/// </summary>
public Color32 ScaleColor =Color.red;
protected override void OnPopulateMesh(VertexHelper vertexHelper)
{
vertexHelper.Clear();
DrawAxis(vertexHelper);
DrawScale(vertexHelper);
}
/// <summary>
/// 绘制坐标轴
/// </summary>
/// <param name="vertexHelper"></param>
protected void DrawAxis(VertexHelper vertexHelper)
{
UIVertex[] vertex = new UIVertex[4];
float xd = 0.05f;
float yd = 0.05f;
int idx = 0;
//左下
vertex[idx].position = new Vector3(Position_O.x - xd, Position_O.y - yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//左上
vertex[idx].position = new Vector3(Position_O.x - xd, Position_O.y + yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右上
vertex[idx].position = new Vector3( XEndPosion.x, Position_O.y + yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右下
vertex[idx].position = new Vector3( XEndPosion.x, Position_O.y - yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
vertexHelper.AddUIVertexQuad(vertex);
idx = 0;
//左下
vertex[idx].position = new Vector3(Position_O.x - xd, Position_O.y - yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//左上
vertex[idx].position = new Vector3(Position_O.x - xd, YEndPosion.y);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右上
vertex[idx].position = new Vector3(Position_O.x + xd, YEndPosion.y);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右下
vertex[idx].position = new Vector3(Position_O.x + xd, Position_O.y - yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
vertexHelper.AddUIVertexQuad(vertex);
}
/// <summary>
/// 绘制刻度
/// </summary>
/// <param name="vertexHelper"></param>
protected void DrawScale(VertexHelper vertexHelper)
{
float[] XScale = new float[XNum];
float[] YScale = new float[YNum];
for (int i = 0;i < XScale.Length-1;i++ )
{
XScale[i] = Position_O.x + ((XEndPosion.x - Position_O.x) / XNum) * (i + 1);
UIVertex[] vertex = new UIVertex[4];
float xd = 0.03f;
float yd = 0.5f;
int idx = 0;
//左下
vertex[idx].position = new Vector3(XScale[i]-xd,Position_O.y);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//左上
vertex[idx].position = new Vector3(XScale[i] - xd, Position_O.y+yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右上
vertex[idx].position = new Vector3(XScale[i] + xd, Position_O.y+yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右下
vertex[idx].position = new Vector3(XScale[i] + xd, Position_O.y);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
vertexHelper.AddUIVertexQuad(vertex);
}
for (int i = 0; i < YScale.Length-1; i++)
{
YScale[i] = Position_O.y + ((YEndPosion.y - Position_O.y) / YNum) * (i + 1);
UIVertex[] vertex = new UIVertex[4];
float xd = 0.5f;
float yd = 0.03f;
int idx = 0;
//左下
vertex[idx].position = new Vector3(Position_O.x,YScale[i]-yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//左上
vertex[idx].position = new Vector3(Position_O.x, YScale[i] +yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右上
vertex[idx].position = new Vector3(Position_O.x+xd, YScale[i]+yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
idx++;
//右下
vertex[idx].position = new Vector3(Position_O.x+xd, YScale[i]-yd);
vertex[idx].color = ScaleColor;
vertex[idx].uv0 = Vector2.zero;
vertexHelper.AddUIVertexQuad(vertex);
}
}
protected void DrawString(VertexHelper vertexHelper)
{
}
}
}