打印时Unicode有不同的宽度
Unicode have different width when printing
我正在用 C#/Unity 开发一个国际象棋引擎,并希望以漂亮的格式打印棋盘。最好我想打印一些 Unicode 片段,但它们最终会使电路板变得不均匀,请参见下图:
正常数字似乎也是如此,因为每一行开始时彼此略有偏离,例如,第 1 行比其他行开始得更靠左。
为什么我的Debug.Log/prints会变成这样,我怎样才能打印出来让每个字符占用相同数量的space?
编辑:
如果有帮助,这是我用于 Debug.Log 开发板的代码:
public static void PrintBitboard(ulong bitboard)
{
string zero = " 0 ";
string one = " 1 ";
string printString = "";
// Loop through all squares and print a 1 if piece and 0 if not a piece on the square
for (int row = 0; row < 8; row++)
{
// Add numbering on the left side
printString += (8 - row) + " ";
for (int col = 0; col < 8; col++)
{
int currentSquare = row * 8 + col;
printString += BitOperations.GetBit(bitboard, currentSquare) != 0 ? one : zero;
}
// Change to new row
printString += "\n";
}
// Print bottom letters
printString += "\n" + " a b c d e f g h";
// Send message to the console
Debug.Log(printString);
}
您要查找的不是“unicode”,而是monospace
-> 正如 GiacomoCatenazzi 已经说过的,唯一负责的是你正在使用的字体
作为“快速而肮脏”的修复/替代方法,您可以尝试简单地使用制表符 (\t
) 而不是空格之类的(通常对于较大的基于字符串的连接,我建议使用 StringBuider
)
public static void PrintBitboard(ulong bitboard)
{
const string zero = "0\t";
const string one = "1\t";
var stringBuilder = new StringBuilder();
// Loop through all squares and print a 1 if piece and 0 if not a piece on the square
for (int row = 0; row < 8; row++)
{
// Add numbering on the left side
stringBuilder.Append((8 - row)).Append('\t');
for (int col = 0; col < 8; col++)
{
int currentSquare = row * 8 + col;
stringBuilder.Append(BitOperations.GetBit(bitboard, currentSquare) != 0 ? one : zero);
}
// Change to new row
stringBuilder.Append('\n');
}
// Print bottom letters
stringBuilder.Append("\n \ta\tb\tc\td\te\tf\tg\th");
// Send message to the console
Debug.Log(stringBuilder.ToString());
}
查看 .Net Fiddle 在 Unity 控制台中的样子
(因为我不知道你使用的是什么 BitOperations
实现,所以我不得不使用一些技巧)
我正在用 C#/Unity 开发一个国际象棋引擎,并希望以漂亮的格式打印棋盘。最好我想打印一些 Unicode 片段,但它们最终会使电路板变得不均匀,请参见下图:
正常数字似乎也是如此,因为每一行开始时彼此略有偏离,例如,第 1 行比其他行开始得更靠左。
为什么我的Debug.Log/prints会变成这样,我怎样才能打印出来让每个字符占用相同数量的space?
编辑: 如果有帮助,这是我用于 Debug.Log 开发板的代码:
public static void PrintBitboard(ulong bitboard)
{
string zero = " 0 ";
string one = " 1 ";
string printString = "";
// Loop through all squares and print a 1 if piece and 0 if not a piece on the square
for (int row = 0; row < 8; row++)
{
// Add numbering on the left side
printString += (8 - row) + " ";
for (int col = 0; col < 8; col++)
{
int currentSquare = row * 8 + col;
printString += BitOperations.GetBit(bitboard, currentSquare) != 0 ? one : zero;
}
// Change to new row
printString += "\n";
}
// Print bottom letters
printString += "\n" + " a b c d e f g h";
// Send message to the console
Debug.Log(printString);
}
您要查找的不是“unicode”,而是monospace
-> 正如 GiacomoCatenazzi 已经说过的,唯一负责的是你正在使用的字体
作为“快速而肮脏”的修复/替代方法,您可以尝试简单地使用制表符 (\t
) 而不是空格之类的(通常对于较大的基于字符串的连接,我建议使用 StringBuider
)
public static void PrintBitboard(ulong bitboard)
{
const string zero = "0\t";
const string one = "1\t";
var stringBuilder = new StringBuilder();
// Loop through all squares and print a 1 if piece and 0 if not a piece on the square
for (int row = 0; row < 8; row++)
{
// Add numbering on the left side
stringBuilder.Append((8 - row)).Append('\t');
for (int col = 0; col < 8; col++)
{
int currentSquare = row * 8 + col;
stringBuilder.Append(BitOperations.GetBit(bitboard, currentSquare) != 0 ? one : zero);
}
// Change to new row
stringBuilder.Append('\n');
}
// Print bottom letters
stringBuilder.Append("\n \ta\tb\tc\td\te\tf\tg\th");
// Send message to the console
Debug.Log(stringBuilder.ToString());
}
查看 .Net Fiddle 在 Unity 控制台中的样子
(因为我不知道你使用的是什么 BitOperations
实现,所以我不得不使用一些技巧)