Search Unity

"Use Sprite Mesh" option recently added to UI Image - What is this for exactly?

Discussion in 'Editor & General Support' started by Saturn1004, Dec 28, 2018.

  1. Saturn1004

    Saturn1004

    Joined:
    Nov 14, 2014
    Posts:
    42
    The documentation for it is here:
    https://docs.unity3d.com/ScriptReference/UI.Image-useSpriteMesh.html

    When I first read it I assumed it's use case was for non-square clickable UI to make the clickable area of the button match the shape instead of always being a square, which would have been a really cool new feature, but after testing it doesn't seem like this is the case.

    So, if this option isn't for making the clickbox of a UI image fit the shape, what is it for exactly?
    Just to reduce overdraw?
    If it's usage case is performance based, should I be using this on UI images with lots of whitespace?

    Or am I just doing something wrong? Is anyone else able to get this option to work for making irregular shaped clickboxes for irregular shaped UI buttons?

    I hate to have to ask this here, but it seems like there is almost zero information on this feature, (which I believe just showed up in 2018.3) and it seems like it could be a useful feature if it's intended usage was known.
     
    lmraddmb likes this.
  2. exzizt

    exzizt

    Joined:
    Sep 30, 2017
    Posts:
    78
    I would also like to know more information about this.
     
    lmraddmb likes this.
  3. HackBeam

    HackBeam

    Joined:
    Feb 6, 2018
    Posts:
    1
    The diference between using this feature or not is as showing in the attached image.
    At left, Use sprite mesh is off (generates a mesh with only two triangles, as always behaviour).
    At right, Use sprite mesh is on (generates more triangles, same as using a sprite renderer).

    So, the generated mesh will react to clicks. Notice that the sprite mesh generated (at right) does not fit the sprite alpha.
    To use the Use sprite mesh feature, you should configure at the sprite import settings > Mesh type > Tight

    This feature is usefull for UI optimization purposes, specially for large images such as backgrounds.
     

    Attached Files:

  4. lucbloom

    lucbloom

    Joined:
    Mar 27, 2020
    Posts:
    42
    If you're looking for a way to do hit-detection on the shape of an image, for instance a UI Button, you can use this class: https://github.com/lucbloom/unity_image_with_hit_detection_on_shape
    It takes the triangle mesh from the sprite texture and matches it with the exact coordinate of the mouse input on top of it.
    Instructions in the readme.md
     
    Nintendo-Silence likes this.
  5. ddadkhah

    ddadkhah

    Joined:
    Nov 6, 2017
    Posts:
    59
    Hi,
    How does it help for UI optimization while It's mesh has more vertices.
     
    Nintendo-Silence likes this.
  6. StinkySteak

    StinkySteak

    Joined:
    Jul 7, 2017
    Posts:
    12
    AFAIK, with 2 triangles we are using transparency which is bad, if we are using more tris but with no transparency, it should be cheaper