Type Mismatch when assigning generated Texture2D to generated TMP Sprite Asset

I’m creating a Twitch Chat Reader with support for twitch emotes. Players would download emotes which would then be packed into a generated texture atlas, which is then assigned to a TMP Sprite Asset that gets generated on runtime. All the generation works but when assigning the texture atlas to the Sprite Asset, I get a type mismatch. I’m able to assign the individual sprites to the Character and Glyph table and they are visible the first time the Sprite Asset is generated. I’m not sure whats causing this issue and changing up the atlas’s properties on initialization or casting it as just a Texture doesn’t fix the problem.

        //This is for generating the sprite Asset
        TMP_SpriteAsset spriteAsset = ScriptableObject.CreateInstance<TMP_SpriteAsset>();
        spriteAsset.name = assetData.name;

        Debug.Log(assetData.spriteData.Length);

        spriteAsset.spriteSheet = atlasTexture as Texture;
        spriteAsset.material = mat;
        Debug.Log(spriteAsset.spriteSheet);
       ///This is for generating the atlas texture
        Texture2D packedTexture = new Texture2D(totalTextureSize, totalTextureSize, TextureFormat.RGBA32, false, true)
        {
            filterMode = FilterMode.Bilinear,
            wrapMode = TextureWrapMode.Clamp
        };

sprite.create()?

Do you write the asset to disk at any point?

The field they’re assigning to is a Texture field: Class TMP_SpriteAsset | TextMeshPro | 3.2.0-pre.10

Both the texture atlas and sprite asset are being written to disk. For the Sprite Asset only the contents of each sprite’s pivot and position are saved as a json text file. The atlas is saved before the sprite asset is generated. As a debug I used AssetDatabase to create an editor only version of the sprite asset, though the issue persists regardless of how the assets saved.

Turns out that the type mismatch when assigning a texture to the sprite asset wasn’t the actual issue, but instead was the material itself. I assigned the sprite atlas texture to it, then assigned the material to the sprite atlas. Then I added each individual sprite to the sprite atlas, which caused the material’s Texture field to be cleared, giving me a white box instead of my sprite (And I thought the type mismatch caused this issue). Assigning the material texture after adding all the sprites fixed it.

1 Like