Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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:
    275
    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:
    275
  3. Stephan_B

    Stephan_B

    Unity Technologies

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