유니티

[ Unity ] TextMeshPro에 Color Gradient를 스크립트로 변경하는 법.

Hexs 2023. 4. 14. 13:16
반응형
public class GoldText : MonoBehaviour
{
    TextMeshPro text;
    public float gold;
    Color alpha;

    Color32[] ul = new Color32[10];
    Color32[] ur = new Color32[10];
    Color32[] dl = new Color32[10];
    Color32[] dr = new Color32[10];
    // Start is called before the first frame update
    void Start()
    {
        // Bloody Mary #ff512f → #dd2476 // Red
        ul[0] = new Color32(255, 81, 47, 255);
        ur[0] = new Color32(221, 36, 118, 255);
        dl[0] = new Color32(255, 81, 47, 255);
        dr[0] = new Color32(221, 36, 118, 255);

        Destroy(this.gameObject, 1.0f);
        text = GetComponent<TextMeshPro>();

        text.text = string.Format("{0:#,##0.##}", gold);
        text.colorGradient = new VertexGradient(ul[2], ur[2], dl[2], dr[2]);

        alpha = text.color;
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(new Vector2(0, 2.0f * Time.deltaTime));
        alpha.a = Mathf.Lerp(alpha.a, 0, Time.deltaTime * 1.0f);
        text.color = alpha;
    }
}

 

TextMeshPro를 사용하면. 글자 색을 단일 색상이 아닌

여러 색상을 섞어서 사용할 수 있는 Color Gradient를 사용할 수 있습니다.



Color Gradient는 에디터 상에서 직접 수정할 수도 있고.

스크립트로 수정할 수도 있습니다.

이런 식으로 단일 색상보다 조금 더 다양한 색감을 줄 수 있습니다?



사용 방법은.

text = GetComponent(); 로 text에 TextMeshPro Component를 불러오고.

text.color Gradient를 사용하여. Gradient 색상을 입력받습니다.

색상을 입력 받을 때 new VertexGradient를 사용하며.

입력값은 Color32로 선언 해줘야 합니다.

VertexGradient의 경우 4개의 인자값을 받는데.

순서대로 VertexGradient( 왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래 ) 입니다.

 

 

반응형