Search Unity

Unity UI alphaHitTestMinimumThreshold on RawImage

Discussion in 'UGUI & TextMesh Pro' started by David_GameDev, Mar 21, 2019.

  1. David_GameDev

    David_GameDev

    Joined:
    Dec 25, 2016
    Posts:
    29
    I am trying to make a Hole in the Unity UI and I wanted to move the Hole with the UV Rect from RawImage, however, I can´t seem to be able to set the alphaHitTestMinimumThreshold for the RawImage, which means I cant click through the Hole.
    Is there any way to achieve this?

    In the Image below, I would like to be able to click through the hole the left top button and not be able to click the right top button. The only very unclean solution I thought of would be making a huge Image with a Hole and putting it in the UI Image texture and moving the whole GameObject, thats why I wanted to ask if there is any other cleaner solution.

    Thanks in advance for any advice.
     

    Attached Files:

  2. David_GameDev

    David_GameDev

    Joined:
    Dec 25, 2016
    Posts:
    29
    I also tried writing a shader which creates a transparent hole which i can move around, however, the clicks also didn´t register through the cutout.
     
  3. w34edrtfg

    w34edrtfg

    Joined:
    Nov 23, 2014
    Posts:
    72
    Quite late, but for google:
    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6.  
    7. /*
    8. * RawImage doesn't have alphaHitTestMinimumThreshold, (which let's you avoid raycasting transparent pixels)
    9. * This class act's as alphaHitTestMinimumThreshold replacement for RawImages by overriding raycast's default behaviour
    10. * THE IMAGE TEXTURE MUST HAVE READ/WRITTING IN IT'S SETTINGS
    11. * Stolen from https://github.com/Hengle/GrassWateringSimulator2019/blob/master/Grass%20Watering%20Simulator%202019/Assets/HTC.UnityPlugin/ViveInputUtility/Examples/4.Teleport/Scripts/ImageAlphaRaycastFilter.cs
    12. */
    13.  
    14. [RequireComponent(typeof(RawImage))]
    15. public class ImageAlphaRaycastFilter : UIBehaviour, ICanvasRaycastFilter
    16. {
    17.     [NonSerialized]
    18.     private RawImage m_rawImage;
    19.  
    20.     public float alphaHitTestMinimumThreshold = 1;
    21.  
    22.     protected RawImage rawImage {
    23.         get { return m_rawImage ?? (m_rawImage = GetComponent<RawImage>()); }
    24.     }
    25.  
    26.     public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) {
    27.         if (alphaHitTestMinimumThreshold <= 0) { return true; }
    28.         if (alphaHitTestMinimumThreshold > 1) { return false; }
    29.  
    30.         var texture = rawImage.mainTexture as Texture2D;
    31.  
    32.         Vector2 local;
    33.         if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform, screenPoint, eventCamera, out local)) {
    34.             return false;
    35.         }
    36.  
    37.         var rect = rawImage.GetPixelAdjustedRect();
    38.  
    39.         // Convert to have lower left corner as reference point.
    40.         local.x += rawImage.rectTransform.pivot.x * rect.width;
    41.         local.y += rawImage.rectTransform.pivot.y * rect.height;
    42.  
    43.         // normalize
    44.         local = new Vector2(local.x / rect.width, local.y / rect.height);
    45.  
    46.         try {
    47.             return texture.GetPixelBilinear(local.x, local.y).a >= alphaHitTestMinimumThreshold;
    48.         } catch (UnityException e) {
    49.             Debug.LogError("Using alphaHitTestMinimumThreshold greater than 0 on Graphic whose sprite texture cannot be read. " + e.Message + " Also make sure to disable sprite packing for this sprite.", this);
    50.             return true;
    51.         }
    52.     }
    53. }
     
    sunBob-2 and David_GameDev like this.