Search Unity

Text Mesh Pro change glow in code / programmatically not working

Discussion in 'UGUI & TextMesh Pro' started by VoodooDetective, Nov 26, 2019.

  1. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    Code (csharp):
    1.  
    2.     public class MainMenuTextHighlighter : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
    3.     {
    4.         // References
    5.         public TextMeshProUGUI text;
    6.  
    7.         public void OnPointerDown(PointerEventData eventData)
    8.         {
    9.             text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 1.0f);
    10.             text.UpdateMeshPadding();
    11.         }
    12.  
    13.         public void OnPointerUp(PointerEventData eventData)
    14.         {
    15.             if (eventData.pointerCurrentRaycast.gameObject == gameObject)
    16.             {
    17.                 text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 0.5f);
    18.                 text.UpdateMeshPadding();
    19.             }
    20.         }
    21.  
    22.         public void OnPointerEnter(PointerEventData eventData)
    23.         {
    24.             text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 1.0f);
    25.             text.UpdateMeshPadding();
    26.         }
    27.  
    28.         public void OnPointerExit(PointerEventData eventData)
    29.         {
    30.             text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 0.5f);
    31.             text.UpdateMeshPadding();
    32.         }
    33.     }
    34.  
    For some reason, when I apply this class to my TMP, it only changes the glow float value to 1.0f and never back to 0.5f. I put in debugs and all these methods are being hit. However, the glow is never reduced back down to 0.5f.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When working with Materials you have to be mindful of whether you are using the material or sharedMaterial property. Accessing the fontMaterial will return an instance of the material whereas accessing the fontSharedMaterial will return the material potential shared by several objects.

    It is possible here that you end up modifying the incorrect material. So make sure you are in fact modifying / applying your changes to the correct material.
     
    VoodooDetective likes this.
  3. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    Hey thanks for the reply, I'm a little confused. So that code above is copied verbatim from my game. I'm seeing the text highlight when I hover or click, but it never goes back to 0.5f glow. My goal is to highlight the individual text element, so I didn't think fontSharedMaterial would be appropriate.

    Is there something in that code that seems off? Would you expect it to glow on hover, and de-glow on exit? Or did I make a mistake somewhere?
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide me with a simple repro test scene / exported package that contains that scene with relevant scripts and resources to allow me to reproduce what you have for testing?
     
    VoodooDetective likes this.
  5. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    Sure! Just put one together. Thanks for offering to take a look. Really really appreciate it. I've been a little confused, but that's not saying much :)
     

    Attached Files:

  6. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    bump for Monday after the holiday
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I finally had a chance to look at the project you provided.

    I imported the project in 2018.4. There appears to be some issues in terms of layout as perhaps the scene and assets were serialized in a newer version of Unity. The events also do not appears to be handled correctly for some reason which I will look into later.

    In the meantime, I did create a new scene with your script which works as expected. Import the attached package in Unity. Make sure the TMP Essential Resources and TMP Examples & Extras have been imported first since the Bangers font asset is contained in those.

    Let me know if the include scene works as expected on your end.

    I added Debug.Log to your event handling which outputs the instance ID of the material you are modifying. As you will see, an instance of the material is created for each text object (4 on this case). The same instance(s) are re-used which is good but still not as efficient as we can do the same thing with only (1) instance.

    Below is a revised script that only uses (1) material instance instead of 1 per text object.

    Code (csharp):
    1.  
    2. using TMPro;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. namespace MainMenu
    6. {
    7.  
    8.     /// <summary>
    9.     /// Handle highlighting main menu text options.
    10.     /// </summary>
    11.     public class MainMenuTextHighlighter : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
    12.     {
    13.         // References
    14.         public TextMeshProUGUI text;
    15.         private static Material m_TextBaseMaterial;
    16.         private static Material m_TextHighlightMaterial;
    17.  
    18.         private void Awake()
    19.         {
    20.             // Get a reference to the default base material
    21.             m_TextBaseMaterial = text.fontSharedMaterial;
    22.  
    23.             // Create new instance of the material assigned to the text object
    24.             // Assumes all text objects will use the same highlight
    25.             m_TextHighlightMaterial = new Material(m_TextBaseMaterial);
    26.  
    27.             // Set Glow Power on the new material instance
    28.             m_TextHighlightMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 1.0f);
    29.         }
    30.  
    31.         public void OnPointerDown(PointerEventData eventData)
    32.         {
    33.             // Assign the modified highlight material
    34.             text.fontSharedMaterial = m_TextHighlightMaterial;
    35.             text.UpdateMeshPadding();
    36.             Debug.Log("OnPointerDown() Material ID: " + text.fontSharedMaterial.GetInstanceID());
    37.         }
    38.  
    39.         public void OnPointerUp(PointerEventData eventData)
    40.         {
    41.             if (eventData.pointerCurrentRaycast.gameObject == gameObject)
    42.             {
    43.                 // Re-assign the default material
    44.                 text.fontSharedMaterial = m_TextBaseMaterial;
    45.                 text.UpdateMeshPadding();
    46.                 Debug.Log("OnPointerUp() Material ID: " + text.fontSharedMaterial.GetInstanceID());
    47.             }
    48.         }
    49.  
    50.         public void OnPointerEnter(PointerEventData eventData)
    51.         {
    52.             // Assign the modified highlight material
    53.             text.fontSharedMaterial = m_TextHighlightMaterial;
    54.             text.UpdateMeshPadding();
    55.             Debug.Log("OnPointerEnter() Material ID: " + text.fontSharedMaterial.GetInstanceID());
    56.         }
    57.  
    58.         public void OnPointerExit(PointerEventData eventData)
    59.         {
    60.             // Re-assign the default material
    61.             text.fontSharedMaterial = m_TextBaseMaterial;
    62.             text.UpdateMeshPadding();
    63.             Debug.Log("OnPointerExit() Material ID: " + text.fontSharedMaterial.GetInstanceID());
    64.         }
    65.     }
    66. }
    67.  
     

    Attached Files:

    VoodooDetective likes this.
  8. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    239
    Ahhhh thanks so much for your help! Turns out this was all happening because of the mask in my scene. I'm still new to unity so I wasn't aware of the trouble it would create for TMP. I guess 2D rect mask is a better choice. Thanks for your help!
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You are most welcome :)
     
    VoodooDetective likes this.
  10. sumitshresthaaitcentre

    sumitshresthaaitcentre

    Joined:
    Dec 21, 2021
    Posts:
    7
    texthighlighter is a freaking life saver i love you