Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Raycast doesn't ignore masked part of an image

Discussion in 'UGUI & TextMesh Pro' started by Anananas42, Sep 21, 2021.

  1. Anananas42

    Anananas42

    Joined:
    May 20, 2021
    Posts:
    6
    Hello,

    I'm trying to create a clickable angle between two lines. I'm using an image with a circular sprite and I'm radially masking out a part of it depending on the current angle.

    The problem is that I can still click on the invisible part.

    I can't find out how to solve this issue. Any help would be much appreciated!

    Here's the code (probably won't help, but just to be sure):
    Code (CSharp):
    1. GameObject angleGraphic = new GameObject("angle", typeof(Image));
    2.             angleGraphic.transform.SetParent(angles, false);
    3.             var imageComponent = angleGraphic.GetComponent<Image>();
    4.             imageComponent.sprite = circleSprite;
    5.             imageComponent.color = color;
    6.             imageComponent.type = Image.Type.Filled;
    7.             imageComponent.fillMethod = Image.FillMethod.Radial360;
    8.             imageComponent.fillClockwise = false;
    9.             imageComponent.fillAmount = angle.Degrees / 360f;
    In an attached script, I'm simply implementing the IPointerClickHandler interface.



    Thanks in advance!
     
  2. Anananas42

    Anananas42

    Joined:
    May 20, 2021
    Posts:
    6
    Found solution! Make the sprite readable and use Image component's property
    Code (CSharp):
    1. imageComponent.alphaHitTestMinimumThreshold = 0.95f;
    This way, only part of the image with pixels having alpha > 0.95f registers interactions.

    Additionally, I have to use my own method and some basic logic to determine if user points over the non-masked part of the image, but this solution is clean enough.
     
    Last edited: Sep 27, 2021