8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityのTextで特定の1文字の座標を取得する方法

Last updated at Posted at 2017-06-13

以下は、UnityEngine.UI.Textの各文字それぞれの座標を取得するコードです。

Text message;

// 中略

IList<UIVertex> verts = message.cachedTextGenerator.verts;
float unitsPerPixel = 1 / message.pixelsPerUnit;
for (int i = 0; i < message.text.Length; i ++) {
	int vertIndex = i * 4;
	UIVertex topLeftVert = verts[vertIndex];
	topLeftVert.position *= unitsPerPixel;
	UIVertex bottomRightVert = verts[vertIndex + 2];
	bottomRightVert.position *= unitsPerPixel;
	Vector3 centerPosition = (topLeftVert.position + bottomRightVert.position) / 2f;
	print (string.Format("「{0}」の中心座標は{1}です", message.text[i], centerPosition));
}

i を指定してあげれば、特定の文字の座標が取得できます。

8
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?