我仅使用DrawLine和for循环就在Unity中制作了8 x 8的网格。 我的下一步是使每个框都有其官方组合,例如,左下角应为1a,左上角应为8a ... 我的计划是在现有循环中再次进行循环,但它只会给出错误。 有人可以给我一些如何实现的提示
private void DrawChessboard()
{
//8 units of 1 meter to the right
Vector3 widthLine = Vector3.right * 8;
//8 units of 1 meter up
Vector3 heightLine = Vector3.forward * 8;
//makes the 8 by 8
for(int i = 0; i <= 8; i++)
{
Vector3 start = Vector3.forward * i;
Debug.DrawLine(start, start + widthLine);
for (int j = 0; j <= 8; j++)
{
start = Vector3.right * j;
Debug.DrawLine(start, start + heightLine);
}
}
//This draws the selection
if (selectionX >= 0 && selectionY >= 0)
{
Debug.DrawLine(
//Bottom left to top right
Vector3.forward * selectionY + Vector3.right * selectionX,
//this is the end point. +1 to make it diagonal
Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
Debug.DrawLine(
//Bottom left to top right
Vector3.forward * (selectionY +1) + Vector3.right * selectionX,
//this is the end point. +1 to make it diagonal
Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
}
}
答案 0 :(得分:0)
我仍然不确定您的方法在哪里正确调用,但是由于您使用Debug.DrawLine
,所以我猜它是某种编辑器方法或脚本。因此,我将在OnDrawGizmos
如前所述,您可以使用Handles.Label(position, "Text")
在SceneView中绘制文本作为调试标签。
为了得到正确的字符,我简单地使用
nextChar = (char)(currentChar + 1);
(要使其完整,我还添加了黑白字段)
所以看起来像
using UnityEditor;
using UnityEngine;
public class ChessFieldDebug : MonoBehaviour
{
private void OnDrawGizmos()
{
//8 units of 1 meter to the right
var widthLine = Vector3.right * 8;
//8 units of 1 meter up
var heightLine = Vector3.forward * 8;
const char firstLetter = 'a';
// Only for you I also added black and white fields
var isBlackField = false;
var black = new Color(0, 0, 0, 0.25f);
var white = new Color(1, 1, 1, 0.25f);
//makes the 8 by 8
//rows
for (var i = 0; i <= 8; i++)
{
var start = Vector3.forward * i;
Debug.DrawLine(start, start + widthLine);
//colums
for (var j = 0; j <= 8; j++)
{
var currentLatter = (char)(firstLetter + j);
start = Vector3.right * j;
Debug.DrawLine(start, start + heightLine);
// this flag alternates between black and white
isBlackField = !isBlackField;
// Since you draw the last line but don't want a field added skip if over 8
if (i >= 8 || j >= 8) continue;
var centerOfField = Vector3.forward * (i + 0.5f) + Vector3.right * (j + 0.5f);
// Draw text label on fields with colum char + row index
Handles.Label(centerOfField, currentLatter.ToString() + (i + 1));
Gizmos.color = isBlackField ? black : white;
Gizmos.DrawCube(Vector3.forward * (i + 0.5f) + Vector3.right * (j + 0.5f), new Vector3(1, 0.01f, 1));
}
}
}
}
结果
请确保将其放置在Editor
文件夹中,或使用诸如此类的预处理器标签
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
public class ChessFieldDebug : MonoBehaviour
{
#if UNITY_EDITOR
private void OnDrawGizmos()
{
// ...
}
#endif
}
避免在构建项目时出现异常