Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Image.alphaHitTestMinimumThreshold Not Working Correctly

Discussion in 'UGUI & TextMesh Pro' started by emrys90, Apr 10, 2017.

  1. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    718
    So I'm trying to use alphaHitTestMinimumThreshold so that players can only drag item icons where there isn't any transparency. It ignores all the transparent regions without any issues at all. The problem is though that there's areas of the image that are not transparent and its ignoring those. I'm using it like this:
    image.alphaHitTestMinimumThreshold = 0.001f;

    How can I fix this?
     
    justcallmeb likes this.
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you set the image to read/write and disable atlasing of the sprite? I've never used this function, but did a quick read through the guide and saw those to point out just in case.
     
    Bakanovskiy95 likes this.
  3. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    718
    I think so. Here's the import settings.
    upload_2017-4-11_9-32-34.png
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That part looks good.. I can take another (wild) guess. Is this sprite/image inside a container or any kind ?
    You could try a higher alpha. In the photo it looks mostly (full / none - though there may be a few pixels on the edges i can't readily see). Try to set the threshold at "0.4f" :)
     
    Bakanovskiy95 likes this.
  5. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    718
    Yes, it is a child of other objects. I've tried it as the root object under a canvas though to see if that made a difference and it didn't.

    My understanding is the lower that number is than the more it includes in the check for valid positions. So a value of 0.001 should include pretty much everything except for what is completely transparent, but it doesn't. My problem is I can have the mouse over the axe blade but it doesn't register it.

    EDIT:
    The source of the method does this:
    return activeSprite.texture.GetPixelBilinear(x, y).a >= alphaHitTestMinimumThreshold;
    So my thinking is either GetPixelBilinear is returning the wrong value, or how it calculates x,y is wrong.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    is that the code verbatim? I think this setting is used to say if the image is "clickable". From the example document I read, they assign a value to an image that is linked (and part of a button).
    Do you assign it like this:
    Code (CSharp):
    1. Image myImage;
    2. void Awake(){
    3.    myImage = GetComponent<Image>();
    4.    myImage.alphaHitTestMinimumThreshold = (value);
    5.    }
    6. // later on, refer to test against myImage.alphaHitTestMinimum ?
    Back to the part about the "0.4f" wouldn't that still be pretty darn accurate on the photo you shared/posted?
    And lastly, if you only want to return a pixel if it's alpha is over a certain value, since you're checking anyways, you could just put the value right there in your check instead of the variable.
     
  7. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    718
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay, sorry you're subclassing Image?
     
  9. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    718
    No, I am not. This is my code.
    GameObject icon = GameObject.Instantiate(PlayerInventoryUI.Instance.ItemIconPrefab);
    icon.name = template.name + " (ItemIcon)";

    Image image = icon.GetComponent<Image>();
    image.sprite = template.Sprite;

    // Do not allow raycasting on fully transparent portions of the image
    image.alphaHitTestMinimumThreshold = 0.001f;
     
  10. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    718
    It appears to be a bug on Unity's end. I copied their code into a component I could use and debug logged the x,y coordinates that it generated in that method, and they were the wrong coordinates for where the mouse was.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol I just spent like 40 mins trying to sort this out.. It was really horrible :)
    You can create your own filter to test for the alpha value of the pixel, but ya.. I couldn't get it to work at all ;) Sorry. haha
     
  12. kilelr

    kilelr

    Joined:
    Apr 16, 2013
    Posts:
    6
    face the same problem which alphaHitTestMinimumThreshold not working... manage to solve it by adding in many 0.01 alpha in photoshop... not sure why
     
  13. Manumoi

    Manumoi

    Joined:
    Oct 31, 2014
    Posts:
    10
    Hello ran into the same issue. Made some tests and as emrys90 said in a previous post, it seems the function receives inadequate mouse coordinates. On the attached screenshot :
    - the blue image size is defined by its anchors but stretched, and I apply alphaHitTestMinimumThreshold on it.
    - Purple points are successful clicks that reach the background image (ie, the raycast is not blocked by the blue image).
    I tried clicking randomly here and there and realised the "mask" location seems to be shifted... I was first testing on version 5.5.1f1, installed to 5.6.1f1 but the problem remains. Hope they fix it soon.
     

    Attached Files:

  14. FunRobDev

    FunRobDev

    Joined:
    Jun 3, 2017
    Posts:
    9
    Same issue here. Seems that pixels reported "shifted" during hit-test as much as the coordinates of the first top-left-most non-transparent pixel in the image. Or coordinates of the bottom-left-most non-transparent pixel.
    So if you put a 2x2 pixel into the top-left corner of your image and also into the bottom-left corner (2x2 again, becaues 1x1 pixel not enough) then then the bug is "hidden" so everything works perfectly.
     
    Cachete likes this.
  15. FunRobDev

    FunRobDev

    Joined:
    Jun 3, 2017
    Posts:
    9
    Hi again! Found a real solution: pixels won't be shifted IF you change the texture's import properties: MeshType = from Tight to FullRect. According to the Unity help on SpriteMeshType is:

    FullRect = Rectangle mesh equal to the user specified sprite size.
    Tight = Tight mesh based on pixel alpha values. As many excess pixels are cropped as possible.

    That explains our problem: if we use Tight, then alpha=0 pixels on the four edges are cut off, this is why the coordinates get shifted by the alphaHitTestMinimumThreshold bug. That's it :)

    Another good advice, what I found out:
    If you create a sprite (or lots of sprites), don't use the default constructor with few parameters, but use the one below, because here you can pass the SpriteMeshType.FullRect at the last parameter, which speeds up sprite creation by 10000%, because I guess, the sprite is not scanned for alpha=0 pixels, just will be create in its full size. And you can avoid the alphaHitTestMinimumThreshold bug again.

    Sprite.Create(
    texture,
    new Rect(0, 0, texture.width, texture.height),
    new Vector2(0, 0),
    pixelsPerUnit:100,
    extrude:0,
    meshType:SpriteMeshType.FullRect); // Note SpriteMeshType.FullRect speeds up +10000%

    God Bless
     
    Last edited: Jul 21, 2017
  16. cwbeta

    cwbeta

    Joined:
    Jan 12, 2017
    Posts:
    30
    Awesome! Solves my problem and the explanation is reasonable!
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Nice to see an answer all this time later :)
     
    Bakanovskiy95 likes this.
  18. Bip901

    Bip901

    Joined:
    May 18, 2017
    Posts:
    71
    Awesome, you saved me :) Love you so much!
     
  19. Zahidylin_Marat

    Zahidylin_Marat

    Joined:
    Jul 28, 2012
    Posts:
    114
    Thanks - it works.
     
  20. Thom_Denick

    Thom_Denick

    Joined:
    May 19, 2014
    Posts:
    13
    Dude, thanks so much.
     
    entropicjoey1 likes this.
  21. temresen

    temresen

    Joined:
    Jul 9, 2018
    Posts:
    29
    God Bless you dude!
     
    entropicjoey1 likes this.
  22. Keseren

    Keseren

    Joined:
    May 8, 2015
    Posts:
    3
    You really helped me! Thanks!
     
  23. Wizmix

    Wizmix

    Joined:
    Aug 16, 2017
    Posts:
    1
    Thanks! You saved some time here bro :)
     
    Robacks likes this.
  24. wasifkhan

    wasifkhan

    Joined:
    Sep 12, 2018
    Posts:
    1
    It works for me.
     
  25. ERICdb

    ERICdb

    Joined:
    Nov 21, 2019
    Posts:
    1

    You are a god sent! I've been trying to fix the problem for the last 2h to no avail. It turns out that your solution is exactly the one i needed. Thank you!
     
    entropicjoey1 likes this.
  26. MrDizzle26

    MrDizzle26

    Joined:
    Feb 8, 2015
    Posts:
    19
    Fixed my issue, you da best!
     
  27. hk1ll3r

    hk1ll3r

    Joined:
    Sep 13, 2018
    Posts:
    88
    Anyone have any idea why the "alphaHitTestMinimumThreshold" field is missing from the documentation since 2019.1? Is this getting deprecated? I can still use it in 2019.4.
     
    lokihornsby and guneyozsan like this.
  28. entropicjoey1

    entropicjoey1

    Joined:
    Jun 1, 2014
    Posts:
    26
    Thank you!
     
  29. Vagabond_

    Vagabond_

    Joined:
    Aug 26, 2014
    Posts:
    1,075
    Aaaannnddd, this does not work with filled images
     
    Fressbrett likes this.
  30. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    60
    Also seems not to work for sliced images
     
    barsukovdn84 likes this.
  31. guneyozsan

    guneyozsan

    Joined:
    Feb 1, 2012
    Posts:
    98
    I'm also here curious of this.
     
  32. Robacks

    Robacks

    Joined:
    Aug 7, 2012
    Posts:
    25
    This is the actual solution to the problem. But still probably unity could fix this.
     
  33. bagmanio

    bagmanio

    Joined:
    Mar 15, 2021
    Posts:
    2
    Ok, guys! I found the following. If you use crunched compression (DXT5 Crunched, ETC Crunched) then
    alphaHitTestMinimumThreshold starts to glitch if you switch to non-crunched texture compression (RGBA, ETC) then it works.
     
  34. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    158
    Wow thanks a million
     
unityunity