Search Unity

TextMesh Pro TMP & UI.Text together

Discussion in 'UGUI & TextMesh Pro' started by Eastwing, Apr 23, 2018.

  1. Eastwing

    Eastwing

    Joined:
    Mar 30, 2013
    Posts:
    26
    Hi there,

    Does TextMeshProUGUI inherit UI.Text? If no, is there any way to use them both in the same method? Common interface maybe?

    I'm trying to refer their text field to replace its string dynamically.

    At the moment I use solution like one below:

    Code (CSharp):
    1. foreach(var go in gos){
    2.             var tmPro = go.GetComponent<TextMeshProUGUI>();
    3.             var uiText = go.GetComponent<Text>();
    4.  
    5.             if(tmPro == null && uiText == null)
    6.                 continue;
    7.  
    8.             if (tmPro != null)
    9.                 tmPro.text = newText;
    10.  
    11.             if (uiText != null)
    12.                 uiText.text = newText;
    13.         }
    but it's pretty ugly, I'd prefer something more elegant
     
    Last edited: Apr 23, 2018
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    TextMesh Pro was designed as a replacement for UI.Text. There is no inheritance between the two.

    The TextMesh Pro components which are <TextMeshPro> and <TextMeshProUGUI> inherit from TMP_Text.

    What is your use case for using both TextMesh Pro and UI.Text?
     
  3. Eastwing

    Eastwing

    Joined:
    Mar 30, 2013
    Posts:
    26
    I've got the project with a lot of UI elements in it. Some of them use TMP, the rest ones use UI.Text. So answer to your question is "original design"
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I believe there are some conversion utilities around written by TMP users to convert projects using UI.Text to TextMesh Pro.