Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question [SpriteMeshPro] Creating TMP_SpriteAsset through code. Material Issue.

Discussion in 'UGUI & TextMesh Pro' started by kindingerlek, Nov 6, 2023.

  1. kindingerlek

    kindingerlek

    Joined:
    Jul 7, 2015
    Posts:
    10
    Hello. I'm trying creating TMP_SpriteAsset by code, and I'm facing only one problem with my code. The material field shows "Type mismatch".

    upload_2023-11-5_22-49-54.png

    The code to create this asset file is from this function:


    Code (CSharp):
    1.  
    2.     private void GenerateTMPSprite(string spriteAssetPath)
    3.     {
    4.         Sprite spritesheet = AssetDatabase.LoadAssetAtPath<Sprite>(spriteAssetPath);
    5.         Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(spriteAssetPath)
    6.             .OfType<Sprite>().ToArray();
    7.  
    8.         TMP_SpriteAsset spriteAsset = CreateInstance<TMP_SpriteAsset>();
    9.         spriteAsset.spriteSheet = spritesheet.texture;
    10.  
    11.         spriteAsset.spriteInfoList = new List<TMP_Sprite>();
    12.         spriteAsset.material = new Material(Shader.Find("TextMeshPro/Sprite"));
    13.  
    14.  
    15.         for (int i = 0; i < sprites.Length; i++)
    16.         {
    17.             Sprite sprite = sprites[i];
    18.             TMP_Sprite spriteData = new()
    19.             {
    20.                 id = i,
    21.                 sprite = sprite,
    22.                 name = sprite.name,
    23.                 x = sprite.textureRect.x,
    24.                 y = sprite.textureRect.y,
    25.                 width = sprite.textureRect.width,
    26.                 height = sprite.textureRect.height,
    27.                 xOffset = sprite.textureRectOffset.x,
    28.                 yOffset = sprite.textureRectOffset.y,
    29.                 xAdvance = sprite.textureRectOffset.x,
    30.                 pivot = new Vector2(0.5f, 0.5f),
    31.                 hashCode = TMP_TextUtilities.GetSimpleHashCode(sprite.name),
    32.             };
    33.  
    34.             spriteAsset.spriteInfoList.Add(spriteData);
    35.         }
    36.  
    37.  
    38.         spriteAsset.UpdateLookupTables();
    39.        
    40.         for(uint i = 0; i < spriteAsset.spriteCharacterTable.Count; i++)
    41.         {
    42.             var spriteCharacter = spriteAsset.spriteCharacterTable[(int)i];
    43.             spriteCharacter.glyphIndex = i;
    44.         }
    45.  
    46.         var assetPath = Path.Combine(Path.GetDirectoryName(spriteAssetPath), Path.GetFileNameWithoutExtension(spriteAssetPath) + ".asset");
    47.         AssetDatabase.CreateAsset(spriteAsset, assetPath);
    48.         AssetDatabase.SaveAssets();
    49.         AssetDatabase.Refresh();
    50.     }
    51.  
    52.  
     
  2. kindingerlek

    kindingerlek

    Joined:
    Jul 7, 2015
    Posts:
    10
    Nevermind. I figured out checking the source code. For future viewers:


    Code (CSharp):
    1.         static void AddDefaultMaterial(SpriteAsset spriteAsset)
    2.         {
    3.             Shader shader = Shader.Find("TextMeshPro/Sprite");
    4.             Material material = new Material(shader);
    5.             material.SetTexture(ShaderUtilities.ID_MainTex, spriteAsset.spriteSheet);
    6.  
    7.             spriteAsset.material = material;
    8.             material.name = spriteAsset.name + " Material";
    9.             AssetDatabase.AddObjectToAsset(material, spriteAsset);
    10.         }