Search Unity

changing Default Sprite Asset in TMP Settings at runtime?

Discussion in 'UGUI & TextMesh Pro' started by Arkorim, Jul 6, 2018.

  1. Arkorim

    Arkorim

    Joined:
    Jan 22, 2014
    Posts:
    11
    Hi,

    I would like to automatically change the Default Sprite Asset located in TMP Settings depending on the target platform.

    Right now I change it manually when building to Switch, PC, Xbox One, etc (I have automated everything else except this). It's easy to forget and an error prone task, so I would like to know how to do it in a proper way.

    Thanks,
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You should be able to change the default sprite asset assigned to the TMP Setting via scripting. This could be done in a prebuild process. https://docs.unity3d.com/ScriptReference/Build.IPreprocessBuildWithReport.OnPreprocessBuild.html
     
  3. Arkorim

    Arkorim

    Joined:
    Jan 22, 2014
    Posts:
    11
    Hi Stephan,

    I suppose you mean changing TMPro.TMP_Settings.defaultSpriteAsset, but it's read only.

    Am I missing something?

    Thanks,
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Oops!

    Assuming it was not ready-only, would that solution work for you?

    BTW: If you are using the package manager version of TMP? This version does include source code so you can make that change yourself. I'll take a look on my end and make this property along with others read-write.
     
  5. Arkorim

    Arkorim

    Joined:
    Jan 22, 2014
    Posts:
    11
    Yes, that would work.

    I'm using Unity 2017.4.4 (and I can't update it). And I'm using TMP Release 1.0.54 (December 2016).
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This release of TMP would be a source code version so, my suggestion is simply for you to modify the properties you need in the TMP_Settings.cs file.
     
  7. Arkorim

    Arkorim

    Joined:
    Jan 22, 2014
    Posts:
    11
    If I look in the Unity project for TMP_Settings I get a reference to this file "TextMeshPro-1.0.55.56.0b9.dll" but no TMP_Settings.cs file.

    Is there a TMP compatible version with Unity 2017.4.4 that includes source code so I can edit the file? or another solution?

    Thanks,
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    As per the above, you are using the Free / dll-only version of TextMesh Pro which doesn't include source code.

    The versions of TextMesh Pro available via the package manager (2018.1) do include source code but those are for 2018.1 and up.

    The previous source code versions of TextMesh Pro use a different set of GUIDs and FileIDs but there was / is no utility to convert project between those two. There is such utility with the new 2018.1 versions of TMP.

    Is upgrading the project to 2018.1 possible for you?
     
  9. Arkorim

    Arkorim

    Joined:
    Jan 22, 2014
    Posts:
    11
    Hi! Thanks for the answer. I can't upgrade it now, but I will change them manually for this project and I will try to upgrade to 2018.1 or above for the next projects.
     
    Stephan_B likes this.
  10. giggioz

    giggioz

    Joined:
    May 11, 2017
    Posts:
    52
    Hi Stephan!

    I had the same issue (but I have another problem, please read below)

    I changed the source code, Visual Studio asks me if I want to rewrite the file (TMP_Settings.cs), I confirm.

    For the ones who need it:

    Code (CSharp):
    1.         /// <summary>
    2.         /// The Default Sprite Asset to be used by default.
    3.         /// </summary>
    4.         //public static TMP_SpriteAsset defaultSpriteAsset
    5.         //{
    6.         //    get { return instance.m_defaultSpriteAsset; }
    7.         //}
    8.         public static TMP_SpriteAsset defaultSpriteAsset
    9.         {
    10.             get { return instance.m_defaultSpriteAsset; }
    11.             set { instance.m_defaultSpriteAsset = value; }
    12.         }
    Everything works fine but when I restart the Editor something restores the original TMP_Settings.cs, I have also tried to modify the file with bash to make it read-only but no success.

    I have a vague memory that in Unity 2018.x.x it did work

    Can you help me with this?
    Thanks!

    Working with
    Unity 2019.1.2f1
    MacOs Mojave 10.14.4
    TextMeshPro 2.0.0
     
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You will need to modify the fill in the global package cache which is located on Window in "C:\Users\my_user_name\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.textmeshpro@..."

    Package are now copied to a local cache inside the Library/PackageCache/..." folder but get re-sync with the global which is why your change gets removed.
     
    giggioz likes this.
  12. giggioz

    giggioz

    Joined:
    May 11, 2017
    Posts:
    52
    It worked, thank you very much.
     
    Stephan_B likes this.
  13. glumleaf

    glumleaf

    Joined:
    Jan 30, 2016
    Posts:
    22
    Hi, did you ever run into any issues making this work in an actual build? I'm using the same exact code and it works perfectly in the Editor, but it doesn't work in a standalone build even though m_defaultSpriteAsset seems to be getting altered properly at runtime.

    @Stephan_B any ideas?