Search Unity

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

Discussion in 'UGUI & TextMesh Pro' started by Alan-Liu, Feb 25, 2022.

  1. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    393
    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?
     
  2. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    393
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    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.

     
  4. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    393
    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:
    Code (CSharp):
    1. TMP_Text.MyClear();
    2. TMP_ResourceManager.RemoveFontAsset(FontAssetToRemove);
    3. MaterialReferenceManager.instance.Clear();
    4.  
    5. // TMP_Text.cs
    6. public static void MyClear()
    7. {
    8.     m_materialReferenceIndexLookup.Clear();
    9.     Array.Clear(m_materialReferences, 0, m_materialReferences.Length);
    10. }
    11.  
    12. // MaterialReferenceManager.cs
    13. public void Clear()
    14. {
    15.     //m_currentIndex = 0;
    16.     // m_MaterialReferenceLookup.Clear();
    17.     m_FontMaterialReferenceLookup.Clear();
    18.     m_SpriteAssetReferenceLookup.Clear();
    19.     m_FontAssetReferenceLookup.Clear();
    20. }
    21.  
    22.  
    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.