如何在不剪切单词的情况下逐个实例化单词到网格

时间:2019-04-01 12:55:40

标签: c# unity3d

我正在做一个游戏,其中玩家会像在命运之轮中减去轮子一样来解决一个短语,但问题是当单词超过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);   
    }
}

实际结果

预期结果

1 个答案:

答案 0 :(得分:0)

只要短语中的任何单词都不能超过该行(在这种情况下,您只需要识别它并用'-'分隔即可)就可以了。

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);
}