상세 컨텐츠

본문 제목

[개발일지] 유니티 UI: Text 출력 안됨 오류 해결

카테고리 없음

by 강자이 2024. 6. 20. 14:46

본문

문제 상황

기존 코드에서 TMP Text를 사용하는 데 문제가 있었습니다. UI_Button 클래스에서 GetText 메서드를 호출하면서도 실제로는 Text 타입의 오브젝트가 반환되고 있었습니다. 이는 TMP Text를 사용하기 위해 수정이 필요한 부분입니다.

 


 

해결 과정

  1. UI_Base 클래스 수정
    • Bind<T> 메서드와 관련된 부분에서는 기존에는 Text 타입을 바인딩하고 있었으나, 이를 TMP_Text로 수정해야 합니다. TMP Text를 사용하기 위해서는 TMP_Text 타입으로 적절하게 바인딩하도록 코드를 수정해야 합니다.
  2. GetText 메서드 수정
    • GetText 메서드를 TMP_Text를 반환하도록 수정합니다. 이렇게 하면 UI_Button 클래스에서 TMP Text를 정상적으로 가져올 수 있습니다.
  3. UI_Button 클래스 수정
    • Start 메서드에서 TMP Text를 가져오는 부분을 수정하여, 새로운 GetText 메서드를 사용하도록 변경합니다.

 

수정전 코드

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI_Button : UI_Base
{
    enum Buttons
    {
        PointButton
    }

    enum Texts
    {
        PointText,
        ScoreText
    }

    enum GameObjects
    {
        //TestObject
    }

    enum Images
    {
        Imeage
    }

    private void Start()
    {
        Bind<Button>(typeof(Buttons));
        Bind<Text>(typeof(Texts));
        Bind<GameObject>(typeof(GameObjects));
        Bind<Image>(typeof(Images));

        GetText((int)Texts.ScoreText).text = "Bind Test";

        GameObject go = GetImage((int)Images.Imeage).gameObject;
        AddUIEvent(go, (PointerEventData data) => { go.transform.position = data.position; }, Define.UIEvent.Drag);
    }
}

 

 

수정후 코드

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;

public class UI_Button : UI_Base
{
    enum Buttons
    {
        PointButton
    }

    enum Texts
    {
        PointText,
        ScoreText
    }

    enum GameObjects
    {
        //TestObject
    }

    enum Images
    {
        Image
    }

    private void Start()
    {
        Bind<Button>(typeof(Buttons));
        Bind<TMP_Text>(typeof(Texts)); // TMP_Text로 변경
        Bind<GameObject>(typeof(GameObjects));
        Bind<Image>(typeof(Images));

        GetText((int)Texts.ScoreText).text = "Bind Test";

        GameObject go = GetImage((int)Images.Image).gameObject;
        AddUIEvent(go, (PointerEventData data) => { go.transform.position = data.position; }, Define.UIEvent.Drag);
    }
}

댓글 영역