Search Unity

TextMesh Pro Changing the "spriteAsset" property at runtime fails if assets only differ by material

Discussion in 'UGUI & TextMesh Pro' started by krajzeg, Jan 24, 2020.

  1. krajzeg

    krajzeg

    Joined:
    Aug 23, 2016
    Posts:
    1
    I wanted to be able to dynamically control the material used for inline sprites in TextMesh Pro text objects. This is needed in order to desaturate the sprites (which is done in a custom shader) when the text is part of a disabled object.

    Since there is (I think) no way to change the material itself, I created two sprite assets which differ by the material stored inside. I then re-assign the "spriteAsset" property at runtime, but this fails to do the job - the TextMeshProUGUI object still visibly uses the old asset.

    As a workaround, this behavior was fixed once I assigned a different sprite atlas to the "disabled" asset. When the two sprite assets use different materials, but the same sprite atlas, the behavior is erratic - the change happens for some objects, but not others.

    I'm changing the sprite asset property like this:
    Code (CSharp):
    1. text.spriteAsset = spriteReplacement;
    2. text.ForceMeshUpdate();
    After the change, the "Extra Settings" tab in the Inspector for this text object shows the correct sprite asset - the new one that was just set.
    upload_2020-1-24_14-19-44.png

    But the TMP Submesh UI component in the same object shows the previous sprite asset still being used, and this is what I see rendered in game view.
    upload_2020-1-24_14-22-7.png

    This is Unity 2019.2.18, TextMesh Pro 2.1.0.preview3 (bug was also present in 2.0.1).