Text Mesh Pro change glow in code / programmatically not working

    public class MainMenuTextHighlighter : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
    {
        // References
        public TextMeshProUGUI text;

        public void OnPointerDown(PointerEventData eventData)
        {
            text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 1.0f);
            text.UpdateMeshPadding();
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (eventData.pointerCurrentRaycast.gameObject == gameObject)
            {
                text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 0.5f);
                text.UpdateMeshPadding();
            }
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 1.0f);
            text.UpdateMeshPadding();
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            text.fontMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 0.5f);
            text.UpdateMeshPadding();
        }
    }

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.

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.

1 Like

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?

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?

1 Like

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 :slight_smile:

5220617–520355–Assets.zip (2.93 MB)

bump for Monday after the holiday

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.

using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
namespace MainMenu
{

    /// <summary>
    /// Handle highlighting main menu text options.
    /// </summary>
    public class MainMenuTextHighlighter : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
    {
        // References
        public TextMeshProUGUI text;
        private static Material m_TextBaseMaterial;
        private static Material m_TextHighlightMaterial;

        private void Awake()
        {
            // Get a reference to the default base material
            m_TextBaseMaterial = text.fontSharedMaterial;

            // Create new instance of the material assigned to the text object
            // Assumes all text objects will use the same highlight
            m_TextHighlightMaterial = new Material(m_TextBaseMaterial);

            // Set Glow Power on the new material instance
            m_TextHighlightMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 1.0f);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            // Assign the modified highlight material
            text.fontSharedMaterial = m_TextHighlightMaterial;
            text.UpdateMeshPadding();
            Debug.Log("OnPointerDown() Material ID: " + text.fontSharedMaterial.GetInstanceID());
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (eventData.pointerCurrentRaycast.gameObject == gameObject)
            {
                // Re-assign the default material
                text.fontSharedMaterial = m_TextBaseMaterial;
                text.UpdateMeshPadding();
                Debug.Log("OnPointerUp() Material ID: " + text.fontSharedMaterial.GetInstanceID());
            }
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            // Assign the modified highlight material
            text.fontSharedMaterial = m_TextHighlightMaterial;
            text.UpdateMeshPadding();
            Debug.Log("OnPointerEnter() Material ID: " + text.fontSharedMaterial.GetInstanceID());
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            // Re-assign the default material
            text.fontSharedMaterial = m_TextBaseMaterial;
            text.UpdateMeshPadding();
            Debug.Log("OnPointerExit() Material ID: " + text.fontSharedMaterial.GetInstanceID());
        }
    }
}

5238890–522776–Text Highlighter.unitypackage (4.1 KB)

1 Like

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!

You are most welcome :slight_smile:

1 Like

texthighlighter is a freaking life saver i love you