(Case 1423283)The font can't be unloaded due to MaterialReferenceManager holds their references

I found OnFontAssetRequest and OnSpriteAssetRequest can be used to load resources by our custom ResourceManager. But I found MaterialReferenceManager hold the loaded resources and never release the references.

I think TMP should provide a way to clear the references manually (and clearing when the scene is unloaded may be also useful). I saw there is a Clear() method in MaterialReferenceManager, but commented, does it cause some issues?

Unity QA reproduced the issue: Unity Issue Tracker - TextMeshPro SpriteAsset is not unloaded when switching scenes

The following function was added to the latest TMP release which is version 3.2.0-pre.3. Below is the relevant reference from the ChangeLog.

Thanks for the reply.
I did a test for TMP_ResourceManager.RemoveFontAsset(), but it seems it’s not enough to unload TMP_FontAsset. Also, TMP_SpriteAsset is not unloaded.

I need to use the following code before loading a new scene to make sure TMP_FontAsset and TMP_SpriteAsset not be referenced:

TMP_Text.MyClear();
TMP_ResourceManager.RemoveFontAsset(FontAssetToRemove);
MaterialReferenceManager.instance.Clear();

// TMP_Text.cs
public static void MyClear()
{
    m_materialReferenceIndexLookup.Clear();
    Array.Clear(m_materialReferences, 0, m_materialReferences.Length);
}

// MaterialReferenceManager.cs
public void Clear()
{
    //m_currentIndex = 0;
    // m_MaterialReferenceLookup.Clear();
    m_FontMaterialReferenceLookup.Clear();
    m_SpriteAssetReferenceLookup.Clear();
    m_FontAssetReferenceLookup.Clear();
}

After applying the code above, Memory Profiler reports a TMP_FontAsset and a TMP_SpriteAsset are unused assets(Before, they are referenced by some static variables). But it’s strange that they are can’t be unloaded using Resources.UnloadUnusedAssets.

I’m using TMP 3.2.0-pre3 and Unity 2020.3.30.

1 Like