「Unity/TMPro/Text」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→色変更) |
(→フォントスタイル変更) |
||
行38: | 行38: | ||
==フォントスタイル変更== | ==フォントスタイル変更== | ||
textPro.fontStyle = FontStyles.Normal; // ノーマルに戻す。uとかbとか色々ある | textPro.fontStyle = FontStyles.Normal; // ノーマルに戻す。uとかbとか色々ある | ||
+ | |||
+ | フォントスタイルの種類 | ||
+ | <pre> | ||
+ | Normal = 0, | ||
+ | Bold = 1, | ||
+ | Italic = 2, | ||
+ | Underline = 4, | ||
+ | LowerCase = 8, | ||
+ | UpperCase = 0x10, | ||
+ | SmallCaps = 0x20, | ||
+ | Strikethrough = 0x40, | ||
+ | Superscript = 0x80, | ||
+ | Subscript = 0x100, | ||
+ | Highlight = 0x200 | ||
+ | </pre> | ||
==テキストの左右の余白を動的に設定する== | ==テキストの左右の余白を動的に設定する== |
2025年8月16日 (土) 08:18時点における最新版
目次
2DのTMProのテキストを追加
- ヒエラルキーで右クリックで、UI/TextMeshProを追加
- Window/TextMeshPro/Import TMP Essential Resources
- (サンプルを使いたい場合)Window/TextMeshPro/Import TMP Example and Extras
2DのTMProのテキストをスクリプトで入れる(uGUI)
using TMPro; public class SampleScene : MonoBehaviour { void Start() { TextMeshProUGUI textPro = GameObject.Find("Text (TMP)").GetComponent<TextMeshProUGUI>(); textPro.text = "hoge"; } }
2DのTMProのテキストをスクリプトで入れる(通常)
using TMPro; public class SampleScene : MonoBehaviour { void Start() { TextMeshPro textPro = GameObject.Find("Text (TMP)").GetComponent<TextMeshPro>(); textPro.text = "hoge"; } }
色変更
textPro.text = "<color=#F1F1F1>□</color>";
スタイル変更
textPro.textStyle = TMP_Style.NormalStyle; // ノーマルに戻す。aとかlinkとか色々ある
フォントスタイル変更
textPro.fontStyle = FontStyles.Normal; // ノーマルに戻す。uとかbとか色々ある
フォントスタイルの種類
Normal = 0, Bold = 1, Italic = 2, Underline = 4, LowerCase = 8, UpperCase = 0x10, SmallCaps = 0x20, Strikethrough = 0x40, Superscript = 0x80, Subscript = 0x100, Highlight = 0x200
テキストの左右の余白を動的に設定する
テキストのRectTransformの"anchor presets"がstretch(その他のmiddleやcenterじゃない)のときで、左右の余白が、0のとき、左右の余白を、10に設定する
TextMeshProUGUI textMesh = transform.GetComponent<TextMeshProUGUI>(); if (textMesh != null) { RectTransform rectTransform = transform.GetComponent<RectTransform>(); bool isCenterOrMiddle = (rectTransform.anchorMin.x == 0.5f && rectTransform.anchorMax.x == 0.5f) || (rectTransform.anchorMin.y == 0.5f && rectTransform.anchorMax.y == 0.5f); // ancher presentが、centerやmiddleのものを除外 if (isCenterOrMiddle) { continue; } // 左右のmarginが0のものについて、marginを10ずつ設定 if (rectTransform.offsetMin.x == 0 && rectTransform.offsetMax.x == 0) { // 左と右のオフセットを設定 rectTransform.offsetMin = new Vector2(10, rectTransform.offsetMin.y); // left rectTransform.offsetMax = new Vector2(-10, rectTransform.offsetMax.y); // right } }