UI Material not updating unless timeScale is 1 when changing CUSTOM shader property through code

After some hair-pulling Googling, I found this thread: Masked UI Element’s Shader Not Updating - Unity Forum
Turns out masks are the issue. It’s a full-on bug too.
Not performant, but for just UI it’s an okay work around… Hope this helps anyone else making UI mask friendly shaders for UI.

New unscaled time passer component code:

using UnityEngine;
using UnityEngine.UI;

[ExecuteAlways]
[RequireComponent(typeof(Image))]
public class PassUnscaledTimeToShader : MonoBehaviour {
    private Image rend;
    private string originalMatName;
    private static readonly int UnscaledTime = Shader.PropertyToID("_UnscaledTime");

    private void Awake() {
        rend = GetComponent<Image>(); //get image component
        originalMatName = rend.material.name; //cache original material name
    }

    void Update() {
        if (rend.material.HasProperty(UnscaledTime)) rend.material.SetFloat(UnscaledTime, Time.unscaledTime);
        else Destroy(this); //Remove if material has no matching property
        rend.material = Instantiate(rend.material) ;//Force mask stencils to update
        rend.material.name = originalMatName; //ensure that (Clone) isn't appended to the end
    }
}
2 Likes