首先,下载一个shapes插件!
为shapes插件的立刻绘画模式创建一个脚本
using Shapes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace XXX
{
[ExecuteAlways]
public class DrawController : ImmediateModeShapeDrawer
{
public Camera cam;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// called by the ImmediateModeShapeDrawer base type
public override void DrawShapes(Camera cam)
{
if (cam != this.cam) // only draw in the player camera
return;
using (Draw.Command(cam))
{
Draw.ZTest = CompareFunction.Less ; // to make sure it draws on top of everything like a HUD //Draw.Matrix = crosshairTransform.localToWorldMatrix; // draw it in the space of crosshairTransform
Draw.BlendMode = ShapesBlendMode.Transparent;
Draw.LineGeometry = LineGeometry.Flat2D;
Draw.Thickness = 0.01f;
//在这里写draw函数
}
}
}
}
public class GridPos
{
public int width;
public int height;
private float cellSize;
public Vector3 originPosition;
public GridPos(int width, int height, float cellSize, Vector3 originPosition)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
this.originPosition = originPosition;
//bool showDebug = false;
//if (showDebug) {
// //TextMesh[,] debugTextArray = new TextMesh[width, height];
// for (int x = 0; x < this.width; x++) {
// for (int y = 0; y < this.height; y++) {
// //debugTextArray[x, y] = UtilsClass.CreateWorldText("", null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter);
// Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
// Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
// }
// }
// Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
// Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
//}
}
public int GetWidth()
{
return width;
}
public int GetHeight()
{
return height;
}
public float GetCellSize()
{
return cellSize;
}
public Vector3 GetWorldPosition(int x, int y)
{
return new Vector3(x, y) * cellSize + originPosition;
}
public Vector3 GetWorldPosition2(int x, int y)
{
return new Vector3(x, y) * cellSize + originPosition + new Vector3(cellSize / 2, cellSize / 2, 0);
}
//GetXY(worldPosition, out x, out y);
public void GetXY(Vector3 worldPosition, out int x, out int y)
{
x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
}
public List GetAllJustPoint()
{
List m_linePoints = new List();
for (int x = 0; x < this.width; x++)
{
for (int y = 0; y < this.height; y++)
{
m_linePoints.Add(GetWorldPosition(x, y));
}
}
return m_linePoints;
}
public List GetAllPoint()
{
List m_linePoints = new List();
for (int x = 0; x < this.width; x++)
{
for (int y = 0; y < this.height; y++)
{
Vector3[] rows = new Vector3[2];
rows[0] = GetWorldPosition(x, y);
rows[1] = GetWorldPosition(x, y + 1);
m_linePoints.Add(rows);
Vector3[] cols = new Vector3[2];
//debugTextArray[x, y] = UtilsClass.CreateWorldText("", null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter);
cols[0] = GetWorldPosition(x, y);
cols[1] = GetWorldPosition(x + 1, y);
m_linePoints.Add(cols);
}
}
Vector3[] rows2 = new Vector3[2];
rows2[0] = GetWorldPosition(0, height);
rows2[1] = GetWorldPosition(width, height);
m_linePoints.Add(rows2);
Vector3[] cols2 = new Vector3[2];
cols2[0] = GetWorldPosition(width, 0);
cols2[1] = GetWorldPosition(width, height);
m_linePoints.Add(cols2);
return m_linePoints;
}
在文件中创建这样一个脚本。
使用这句话创建一个网格
GridPos gp1 =new GridPos((int)width, (int)height, 1, new Vector3(0.5f, 0.5f, 0) + offposition);
List m_linePoints_point = gp1.GetAllPoint();//这是要绘画出来的点
然后用下列语句添加在 DrawController 中,进行绘制:
for (int i = 0; i < m_linePoints_point.Count; i++)
{
//Debug.Log(m_linePoints[i][0]);
Draw.Line(m_linePoints_point[i][0], m_linePoints_point[i][1], UnityEngine.Color.gray);
}