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;
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.
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”
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.
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:
Image myImage;
void Awake(){
myImage = GetComponent<Image>();
myImage.alphaHitTestMinimumThreshold = (value);
}
// 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.
No, I am not. This is my code.
GameObject icon = GameObject.Instantiate(PlayerInventoryUI.Instance.ItemIconPrefab);
icon.name = template.name + " (ItemIcon)";
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.
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
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.
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.
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%