Search Unity

Find and load all TextMeshProUGUI / TMP_FontAsset into list issue

Discussion in 'UGUI & TextMesh Pro' started by Area1, Apr 20, 2018.

  1. Area1

    Area1

    Joined:
    Dec 13, 2014
    Posts:
    5
    // Unity UI Font work fine
    string[] getGuids = UnityEditor.AssetDatabase.FindAssets("t:Font");
    foreach (string guid in getGuids)
    {
    Debug.Log("label lookup: " + UnityEditor.AssetDatabase.GUIDToAssetPath(guid));
    string name = Path.GetFileNameWithoutExtension( UnityEditor.AssetDatabase.GUIDToAssetPath(guid) );
    Font font = new Font(name);
    fontList.Add(font);
    }

    // Unity TMP_FontAsset does NOT work, or not sure how to get into ctor TMP_FontAsset
    string[] getGuids2 = UnityEditor.AssetDatabase.FindAssets("t:TMP_FontAsset");
    foreach (string guid in getGuids2)
    {
    string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid));
    Debug.Log("label lookup: " + path;
    string name = Path.GetFileNameWithoutExtension(UnityEditor.AssetDatabase.GUIDToAssetPath(guid));
    TMP_FontAsset font = new TMP_FontAsset(); // No parameter possible to add, like in new Font(name);
    // What do to here, to load/assign FontAsset? Any ideas?

    tmpFontAssetList.Add(font);
    }
    // Thanks!!!
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The following line of code should work fine.

    Code (csharp):
    1. string[] fontAssets = UnityEditor.AssetDatabase.FindAssets("t:TMP_FontAsset");
    Do you have any TMP_FontAssets in the project?

    When you type this in the Project Search toolbar do any TMP_FontAsset get listed?
    upload_2018-4-19_23-38-40.png
     
  3. Area1

    Area1

    Joined:
    Dec 13, 2014
    Posts:
    5
    Yes that part works. I am not sure now to assign the proper TMP_FontAsset to the list.

    string name = Path.GetFileNameWithoutExtension(UnityEditor.AssetDatabase.GUIDToAssetPath(guid));
    TMP_FontAsset font = new TMP_FontAsset(); // No parameter possible to add, like in new Font(name);
    // What do to here, to load/assign FontAsset? Any ideas?

    Thanks!
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Font Assets are resources and as such must be located in a Resources folder in the sub folder defined in the TMP Settings files.

    In terms of assigning a new font asset to a text object, take a look at some of the example scripts like "TMP_ExampleScript_01.cs" included with TextMesh Pro.

    Code (csharp):
    1.  
    2. // Load a new font asset and assign it to the text object.
    3. m_text.font = Resources.Load<TMP_FontAsset>("Fonts & Materials/Anton SDF");
    4.  
    5. // Load a new material preset which was created with the context menu duplicate.
    6. m_text.fontSharedMaterial = Resources.Load<Material>("Fonts & Materials/Anton SDF - Drop Shadow");