見出し画像

「RotateText.cs」がバグった話~盲点だったスペース~

あらすじ


Textを簡単に縦書きに出来る「RotateText.cs」を使ったtextで、制作物で回転しない文字+スペース+回転しない文字で入力を行った際、回転しない文字の最初がなぜか回転してしまうのでなんとか修正してみました。

今回利用させていただいたサイト様


修正ポイント

サンプル画像を用意いたしました。
横文字で「Brave The Lion2」となれば成功です。

修正前

「T」と「i」が回転してしまっています。

修正後


すべてが完全に横文字になっています。

追加箇所

「RotateText」内関数のModifyVertices()の中にスペースを取り除く文を追加いたしました。void ModifyVertices(List<UIVertex> vertexList)を下のコードに入れ替えたら動くと思います。

void ModifyVertices(List<UIVertex> vertexList)
    {
        if (textComponent != null)
        {
            if (textComponent.text != null && textComponent.text != "")
            {
                // スペースを取り除く
                string characterStringPlane = textComponent.text;
                string characterString = characterStringPlane.Replace(" ", "");

                characters = characterString.ToCharArray();
                if (characters.Length == 0)
                {
                    return;
                }

                int vertexListCount = vertexList.Count;
                for (int i = 0; i < vertexListCount; i += 6)
                {
                    int index = i / 6;
                    //文字の回転の制御
                    if (!IsNonrotatableCharactor(characters[index]))
                    {
                        var center = Vector2.Lerp(vertexList[i].position, vertexList[i + 3].position, 0.5f);
                        for (int r = 0; r < 6; r++)
                        {
                            var element = vertexList[i + r];
                            var pos = element.position - (Vector3)center;
                            var newPos = new Vector2(
                                            pos.x * Mathf.Cos(90 * Mathf.Deg2Rad) - pos.y * Mathf.Sin(90 * Mathf.Deg2Rad),
                                            pos.x * Mathf.Sin(90 * Mathf.Deg2Rad) + pos.y * Mathf.Cos(90 * Mathf.Deg2Rad)
                                        );
                            element.position = (Vector3)(newPos + center);
                            vertexList[i + r] = element;
                        }
                    }
                    //文字の位置の制御
                    float[] shiftPixel = GetPixelShiftCharactor(characters[index]);
                    if (shiftPixel[0] != 0 || shiftPixel[1] != 0)
                    {
                        var center = Vector2.Lerp(vertexList[i].position, vertexList[i + 3].position, 0.5f);

                        char character = characters[index];
                        int roop = 6;
                        for (int r = 0; r < roop; r++)
                        {
                            var element = vertexList[i + r];
                            Debug.Log("before:" + element.position.x + "," + element.position.y);
                            var pos = element.position - (Vector3)center;
                            var newPos = new Vector2(
                                            pos.x + shiftPixel[0],
                                            pos.y + shiftPixel[1]
                                        );
                            element.position = (Vector3)(newPos + center);
                            Debug.Log("after:" + element.position.x + "," + element.position.y);
                            vertexList[i + r] = element;
                        }

                    }
                }
            }
        }
    }


この記事が気に入ったらサポートをしてみませんか?