如何在没有被剪切的单词的情况下逐个字母地将单词实例化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在没有被剪切的单词的情况下逐个字母地将单词实例化相关的知识,希望对你有一定的参考价值。
我正在制作一个游戏,玩家会像在命运之轮中减去方向盘一样解决一个短语,但问题是当单词超过15列时,某些单词会被删除。有没有办法让这个词充满下一行?
我只用我知道为每个字母制作一个瓷砖的尝试
我似乎无法在网上找到任何解决方案,我只是开始学习团结
private const int Columns = 15;
private const float Space = 35.0f;
public void LettertoBox()
{
for (int i = 0; i < currentWord.Length; i++)
{
int row = i / Columns;
int column = i % Columns;
Vector3 newPosition3;
newPosition3 = new Vector3((cen.transform.position.x + column * Space), (((cen.transform.position.y )) - row * Space), cen2.transform.position.z);
if (currentWord[i].ToString() != " ")
{
GameObject k = (GameObject)Instantiate(letterimage, newPosition3, Quaternion.identity);
k.name = "letterimage" + (i + 1);
k.transform.SetParent(GameObject.Find("Canvas").transform);
}
else
{
GameObject k = (GameObject)Instantiate(noletterimage, newPosition3, Quaternion.identity);
k.name = "letterimage" + (i + 1);
k.transform.SetParent(GameObject.Find("Canvas").transform);
}
GameObject l = (GameObject)Instantiate(letter, newPosition3, Quaternion.identity);
l.name = "letter" + (i + 1);
l.transform.SetParent(GameObject.Find("Canvas").transform);
}
}
实际结果
预期结果
答案
只要短语中没有任何单词对于行太大(这种情况下你只需要识别它并用' - '拆分),这应该适合你。
public void LetterToBox() {
int rowCount = 0;
int position = 0;
foreach (string word in phraseToSplitToWords.Split(" ")) {
if (CannotFitWordWithoutCuttingPastTheLimit(position,word, whateverIsTheNumberOfLettersPerLineYouWant)) {
rowCont++;
position= 0;
}
PlaceTheWordLetterByLetterInThisPosition(position,row,word);
position = position + word.Length +1; //+1 for the space between words
}
}
public void PlaceTheWordLetterByLetterInThisPosition(int positionInLine, int row, string word) {
foreach (char letter in word) {
var x = positionInLine* LETTER_WIDTH;
var y = row* LETTER_HEIGHT;
InstantiateLetter(x, y, letter); //basically the same way you did
}
}
public bool CannotFitWordWithoutCuttingPastTheLimit(int position, string word, int limit) {
return (position + word.Length > limit);
}
以上是关于如何在没有被剪切的单词的情况下逐个字母地将单词实例化的主要内容,如果未能解决你的问题,请参考以下文章
为啥超出单行 TextView 边界的单词不会被渲染和剪切?