Search Unity

Question Runtime Changes to Default Sprite Asset not working in Standalone Build

Discussion in 'UGUI & TextMesh Pro' started by glumleaf, Jan 4, 2022.

  1. glumleaf

    glumleaf

    Joined:
    Jan 30, 2016
    Posts:
    22
    I'm trying to change the Default Sprite Asset to display different inline sprites depending on the current input device. I'm using the solution Stephan_B suggests in this thread, and it works perfectly but only in the Unity Editor.

    When the Default Sprite Asset is changed in a build, everything works fine except for the inline sprites actually swapping to reflect the Sprite Asset being changed. I have checked to make sure that all of my input device detection is working properly, and I even made sure that instance.m_defaultSpriteAsset in TMP_Settings.cs is being updated to the correct Sprite Asset when the input device is changed.

    Any idea why this works in the Editor but not in a Build? Am I forgetting to rebuild some aspect of TextMeshPro or something?
     
  2. glumleaf

    glumleaf

    Joined:
    Jan 30, 2016
    Posts:
    22
    Bumping this because I still can't seem to figure out the issue :(
     
  3. GrimnirTheWhite

    GrimnirTheWhite

    Joined:
    Feb 9, 2019
    Posts:
    3
    That is not very conventional method to do it since we are editing the source code for text mesh pro but
    • Edit TMP_Settings.cs in editor and also the cache file that you can find at C:\User\*UserName*\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.textmeshpro@*whateverversion*\Scripts\Runtime TMP_Settings.cs and edit around line 240 . So that when code edited unity registers as valid packet.

      Code (CSharp):
      1.         /// <summary>
      2.         /// Disables InternalUpdate() calls when true. This can improve performance when the scale of the text object is static.
      3.         /// </summary>
      4.         public static bool isTextObjectScaleStatic
      5.         {
      6.             get { return instance.m_IsTextObjectScaleStatic; }
      7.             set { instance.m_IsTextObjectScaleStatic = value; }
      8.         }
      9.         [SerializeField]
      10.         private bool m_IsTextObjectScaleStatic;
      After doing this you will be able to set default sprite at runtime via


      public TMP_SpriteAsset YOURSPRITE;
      TMP_Settings.defaultSpriteAsset = YOURSPRITE;


      There is couple of ways to do it but in my case it is not huge amount of sprites to change via
      playerInput.currentControlScheme;

      or
        InputSystem.onDeviceChange -= OnDeviceChange;


      After you detect which input you have you can simply get all Text and assign sprite at runtime without having any trouble as below. I am using 2 sprite asset with individual sprite sheet
      upload_2022-12-14_17-29-31.png
    Code (CSharp):
    1.     private void SetControls(string ControlType)
    2.     {
    3.         //You can pass whatever variable is to conditions. In my case its just string of playerInput.currentControlScheme;
    4.         //Get all TextMeshProUGUI gameobjects
    5.         TextMeshProUGUI[] SpriteComponents = GameObject.FindObjectsOfType<TextMeshProUGUI>();
    6.      
    7.         //Set Default Sprites to Use
    8.         if (ControlType == "Keyboard")
    9.         {
    10.             TMP_Settings.defaultSpriteAsset = KeyboardSprites;
    11.             Debug.Log("Using Keyboard/Mouse Scheme");
    12.         }
    13.         else if (ControlType == "Gamepad")
    14.         {
    15.             TMP_Settings.defaultSpriteAsset = GamepadSprites;
    16.             Debug.Log("Using Controller Scheme");
    17.         }
    18.  
    19.         //Set Default assets of the components in runtime in hierarchy.
    20.         foreach (var SpriteComponent in SpriteComponents)
    21.         {
    22.             SpriteComponent.spriteAsset = TMP_Settings.defaultSpriteAsset;
    23.         }
    24.     }
    25. }
    note : you can do this in several methods