UnityのUI.Text
で、実際にテキストを表示した際のサイズが知りたい場合はpreferredWidth
やpreferredHeight
を使って調べることができるそうです。
使い方
Text text;
float width = text.preferredWidth;
float height = text.preferredHeight;
テキストのサイズに合わせてUIを調整したい場合に便利かなと思います。
TextMeshPro
TexhMeshProの場合もpreferredWidth
とpreferredHeight
を使って実際のサイズを調べることができるようです。
使い方
TMPro.TextMeshProUGUI text;
float width = text.preferredWidth;
float height = text.preferredHeight;
リンク
Unity – Scripting API: ILayoutElement
https://docs.unity3d.com/ScriptReference/UI.ILayoutElement.html
Class TextMeshProUGUI | TextMeshPro | 1.5.6
https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.5/api/TMPro.TextMeshProUGUI.html
関連記事
UIとGameObjectが重なっている場合の判定方法
Raycastを使ってGameObjectの選択をしているプログラムで、UIとGameObjectが重なっている場合に使える方法。
using UnityEngine.EventSystems
EventSystem.current.IsPointerOverGameObject()
リンク
How to make UI block raycats (mobile) | Un...
UnityのUIでDrag and Drop
IBeginDragHandler, IDragHandler, IDropHandlerを継承するとOnBeginDrag, OnDrag, OnDropが使えようになるそうです。
public class Example : MonoBehaviour, IBeginDragHandler, IDragHandler, IDropHandler
{
public void OnBe...
UnityのUIでフォーカスを移したい場合はSetSelectedGameObject
UnityのUIを使っていて、フォーカスを変えたい場合はEventSystem.SetSelectedGameObjectを使ってみると良いみたいです。
使い方
EventSystem.current.SetSelectedGameObject(gameObject);
Canvasを2重に表示していて、後ろ側になっているCanvasのボタンにフォーカスがある場合など、一見不可...
TextMesh ProにSpriteを表示する方法
TextMesh ProはUI上にテキストを表示するAssetですが、タグを使って画像を表示することもできます。
手順としては次のような感じです。
1. 表示したい画像ファイルを読み込み、Sprite ModeをMultipleに設定します。
2. Spriteを右クリックしてCreate » TextMeshPro » Sprite Assetを実行します。
...