3
0

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 1 year has passed since last update.

Unity:スクリプト上からTextMeshProを変更する

Last updated at Posted at 2023-09-13

はじめに

UnityでTextMeshProを作成するとき
3D Object→Text TextMeshPro
UI→Text TextMeshPro
の二種類あって、スクリプト上から色を変更したいときの方法がそれぞれ違うっぽいのでメモ。
サンプルコードはTextMeshProのオブジェクトにアタッチして使うやつ。

3D Objectから作成したものにアタッチする時のコード

Example.cs
using TMPro;
using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        //コンポ取得
        TextMeshPro textmeshPro = GetComponent<TextMeshPro>();

        // 現在のカラーを取得
        Color currentColor = textmeshPro.color;

        // 新しいアルファ値を設定
        currentColor.a = 0.5f; // 例として0.5fに設定

        // 設定した新しいカラーを適用
        textmeshPro.color = currentColor;
    }

UIから作成したものにアタッチする時のコード

Example2.cs
using TMPro;
using UnityEngine;

public class Example2 : MonoBehaviour
{
    private void Start()
    {
        // コンポ取得
        TextMeshProUGUI textMeshPro = GetComponent<TextMeshProUGUI>();

        // 現在のカラーを取得
        Color currentColor = textMeshPro.color;

        // 新しいアルファ値を設定
        currentColor.a = 0.5f; // 例として0.5fに設定

        // 設定した新しいカラーを適用
        textMeshPro.color = currentColor;
    }

}

まとめ

「3D Object」から作ったTextMeshProは

GetComponent<TextMeshPro>();

「UI」から作ったTextMeshProは

GetComponent<TextMeshProUGUI>();

ってことみたい。

3
0
0

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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?