Search Unity

Creating TMP_SpriteAsset Runtime

Discussion in 'UGUI & TextMesh Pro' started by G0nZ, Jan 21, 2019.

  1. G0nZ

    G0nZ

    Joined:
    Jun 7, 2017
    Posts:
    1
    I need to know if there is a way to create a SpriteAsset Runtime.

    I'm receiving an image from a server, so if I want to display this image inside a TMP text component, I need to create the spriteAsset and assign this one inside the text runtime.

    Is this possible ?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Yes.

    Sprite Assets contain a data structure that includes a list of sprites, a material and texture as sub assets.

    The sprites defined in the Sprite Asset simply reference the location of the image data that should correspond to these sprites / images in the texture. The sprite asset has no idea whether or not these texture coordinates actually contain pixels that make up some sprite or just a bunch of black or random pixels. Ie. You could start with an empty texture that gets populated / written into at runtime. As long as where you end up writing these sprites in the texture matches what you defined ahead of time, TMP will just render whatever happens to be contained in the texture at the defined sprite location.

    When creating these sprite assets, you would pre-define the location of these sprites. For instance, assuming the images from the serve are 256 x 256, you would create the sprite asset with pre-define sprite location based on some grid that is 256 x 256.

    In the example above, I am pre-defining these sprites but the sprite data in the Sprite Asset could also be changed at runtime which would then allow you to write them at different locations in the texture and them make sure the sprite references the correct coordinates, width, height, etc.
     
    viesc123 likes this.