Search Unity

Question Having trouble with scaling non canvas text

Discussion in 'UGUI & TextMesh Pro' started by darkjust1, Mar 25, 2024.

  1. darkjust1

    darkjust1

    Joined:
    Sep 27, 2018
    Posts:
    3
    Hello, I have been working on a 2d top down game where enemies drop loot when destroyed. I have item labels where the item is dropped and when clicked it goes into the player's inventory. I have a camera that moves and zooms in and out. Additionally, since these drops are instantiated when an enemy is destroyed(at enemy position), I am not instantiating these onto the canvas. I want the background(sprite) to scale with the text(textmeshpro) and the text and background to scale with the orthographic size of my camera. I am able to scale the sprite but have been having trouble with the text. Also I was having trouble when I tried to use content size fitters/horizontal layouts to scale the background with the text. Id appreciate any suggestions on how to tackle this problem. Thanks.

    Screenshots
    upload_2024-3-25_17-29-41.png
    upload_2024-3-25_17-30-18.png
     
  2. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    if it's intended for 3d, did you consider TMPro 3d component?: https://docs.unity3d.com/Packages/com.unity.textmeshpro@3.2/manual/TMPObject3DText.html

    I honestly didn't use it, but in my new projects I will start using them. Fiddling with canvases and such isn't worth my time. I'd only keep a 2D Canvas for On-screen UI or at most the World-canvas for stuff like sci-fi 3d interfaces, but not titles of objects in the 3d world. I say it's worth considering
     
  3. darkjust1

    darkjust1

    Joined:
    Sep 27, 2018
    Posts:
    3
    I took a look at the 3d component and tried it out, I still seem to have an issue though. I want to scale the text with the background image, but the image is a sprite. The only way I know how to 'Resize' a sprite is to stretch it. When I stretch the parent it stretches the text as well.

    upload_2024-3-27_14-13-0.png

    All I'm trying to do is create a similar effect to diablo/poe or loot based arpgs where the item has text above the background.
    upload_2024-3-27_14-15-9.png

    Is my approach to this wrong? Should these be grouped as ui elements onto the canvas instead and then translated into a world position?
     
  4. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    by stretching you mean affecting transform.localScale?
    but yeah, if you want that, I assume you want them to all be of the same size regardless of the distance to their corresponding entity, so a WorldSpace canvas for each Box + TMProUGUI pair would be needed in that case (or at least that one way of doing it). and then the usual canvas sizing/scaling rules apply, where there TMProUGUI won't be stretching as you'll be modifying the parent box's rect, not its scale.

    I haven't tested the performance of this, as last time I spend some time (more than I should) to Unify all texts into a ScreenSpace UI, but I think there are better ways, like the one described above.
    Then, to keep all texts the same size, have a script that sets their scale based on their distance to the camera, and ofc that makes them 'look at' the camera
     
    darkjust1 likes this.
  5. darkjust1

    darkjust1

    Joined:
    Sep 27, 2018
    Posts:
    3
    Appreciate the feedback, I ended up just creating another canvas and set it to world space and then scaled the drops based on the orthographic size and local scale. The only thing that's kind of annoying is that i have to specify the material so it doesn't get effected by post processing. I wish there was a way to just avoid using a layer instead of material.

    upload_2024-3-31_14-18-16.png
     
    Last edited: Mar 31, 2024
    xucian likes this.