Search Unity

How to draw text on top of one sprite and below another sprite?

Discussion in '2D' started by jslind, May 26, 2022.

  1. jslind

    jslind

    Joined:
    Jul 12, 2013
    Posts:
    4
    Hello,

    So basically, I can't figure out how to draw text (Text or Text Mesh Pro) below/under a Sprite. So that the Sprite appears on top of the Text.

    Also, it would have to be so that the Text is on top of some Sprites, yet under some other Sprites. But as I said, I can't even figure out how to draw the Text below ANY Sprites. Here's a picture to visualize the problem:



    I do know how to sort Sprites amongst themselves to draw them in the order I want (using SortingOrder), but just can't get Text nor TMP's (tried with both) to not appear on top of everything else.

    I've tried to mess around with Layers, SortingLayers and SortingOrders of the Sprites and the Canvas (that holds the Texts), but nothing. The Canvas - and thus the Texts - just always remains on top of all the Sprites.

    I'd prefer if this would be doable with the UI Text components, as that's what I've been using in my project, but if it's only doable with Text Mesh Pro, then that's fine at this point - anything goes as long as I'd just get that to work the way described in the picture. In script (C#) is fine, even preferable.

    Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Three (3) ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    Additional reading in the official docs:

    https://docs.unity3d.com/Manual/2DSorting.html
     
    masterton likes this.
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hello,

    If you change the canvas's Render Mode to World Space, the "Order in Layer" setting essentially becomes like a sprite's sorting order.
     
    jslind likes this.
  4. jslind

    jslind

    Joined:
    Jul 12, 2013
    Posts:
    4
    Thank you very much, this was the problem - I had it as "Screen Space - Overlay". Now that I think about it, the "Overlay" kind of should have made it obvious that it'll stay on top of everything no matter what...

    So now I have the main Canvas as "World Space", and then I attach a new Canvas to each Text object and set them to "Override Sorting: True" and then just give them a Sorting Order like I give to the Sprites, and it seems to work just like I wanted now.
     
    Unrighteouss likes this.
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    There are two types of textmesh pro components: TextMesh Pro - Text(UI) and TextMesh Pro - Text. First one is for canvas and it's layering will depend on canvas settings, the second one functions as in world object and has all the same sorting layer and settings as sprite renderers without requiring to create separate canvas for each text object.

    It will depend on your project needs whether its more convenient to change sorting order for whole canvas with multiple text objects or change the sorting settings for each text object separately.
     
    Unrighteouss, jslind and Kurt-Dekker like this.