gridSize를 입력받아 n x n 사이즈의 격자를 그리도록 했다.
변수
👉 gridSize : 격자 크기
👉 floorIndex : 오브젝트를 그릴 층
👉 floorSpaceSize : 층 사이 간격
void DrawGrid()
{
Handles.color = Color.cyan;
Handles.zTest = UnityEngine.Rendering.CompareFunction.Less; // 🐸
var verticalOffset = Vector3.up * floorIndex * floorSpaceSize;
var gridCenterToTilemap = (editTilemap.transform.position + (new Vector3(1f, 0f, 1f) * floorSpaceSize * gridSize * 0.5f))
- editTilemap.transform.position;
for (int lineIndex = 0; lineIndex <= gridSize; ++lineIndex)
{
var sideOffsetAmount = lineIndex;
var start = editTilemap.transform.position + (Vector3.right * sideOffsetAmount);
var end = editTilemap.transform.position + (Vector3.forward * gridSize)
+ (Vector3.right * sideOffsetAmount);
Handles.DrawLine(start + verticalOffset - gridCenterToTilemap, end + verticalOffset - gridCenterToTilemap);
start = editTilemap.transform.position + (Vector3.forward * sideOffsetAmount);
end = editTilemap.transform.position + (Vector3.right * gridSize)
+ (Vector3.forward * sideOffsetAmount);
Handles.DrawLine(start + verticalOffset - gridCenterToTilemap, end + verticalOffset - gridCenterToTilemap);
}
}
🐸 오브젝트 위에 격자가 보이지 않게
Handles.zTest = UnityEngine.Rendering.CompareFunction.Less;
해당 방식으로 격자를 그렸을때 문제가 하나 발생했다.
gridSize가 짝수일 때 그리드가 원하는 위치에 생성되지 않았다.
for (int lineIndex = 0; lineIndex <= gridSize; ++lineIndex)
{
var sideOffsetAmount = lineIndex;
var calcOffset = gridSize % 2 == 0 ? 0.5f : 0f;
var start = editTilemap.transform.position + (Vector3.right * (sideOffsetAmount + calcOffset)) + (Vector3.forward * calcOffset);
var end = editTilemap.transform.position + (Vector3.forward * (gridSize + calcOffset))
+ (Vector3.right * (sideOffsetAmount + calcOffset));
Handles.DrawLine(start + verticalOffset - gridCenterToTilemap, end + verticalOffset - gridCenterToTilemap);
start = editTilemap.transform.position + (Vector3.forward * (sideOffsetAmount + calcOffset)) + (Vector3.right * calcOffset);
end = editTilemap.transform.position + (Vector3.right * (gridSize + calcOffset))
+ (Vector3.forward * (sideOffsetAmount + calcOffset));
Handles.DrawLine(start + verticalOffset - gridCenterToTilemap, end + verticalOffset - gridCenterToTilemap);
}
calcOffset 변수를 추가하여 gridSize가 짝수일때는 0.5칸씩 옮겨서 그려질수 있도록 했다.
정상적으로 잘 그려진다.
참고
👀 https://velog.io/@gold715/Map-tool-%EA%B5%AC%ED%98%841
👀 https://junwe99.tistory.com/39
👀 https://bloodstrawberry.tistory.com/1063
👀 https://assetstore.unity.com/packages/tools/level-design/fabgrid-level-editor-175642