Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Layar · Nov 26, 2015 at 03:28 PM · raycastingtransparencyalphaui imagetransparent

Raycasting through UI Image with transparent part

Hi,

I use the new UI system (on unity 5.2.2) and I have an UI Image that contains a 2D sprite. This sprite have some transparent parts and looks like this : alt text

(in gray transparent parts, in orange colored parts)

I use Physics.Raycast to select my gamesObjects in the scene and I check if my pointer isn't on a UI element (with EventSystem). But my problem is that EventSystem.IsPointerOverGameObject() return true on gray (transparent) parts of my sprite.How can I select 3D elements through transparent parts of my UI elements.

Thanks for help.

some code :


if (Input.GetButtonDown("Mouse1"))
{
   if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out selectionRay) &&
   !eventSystem.IsPointerOverGameObject())
   {
       Debug.Log("Select game object "+ selectionRay.collider);
   }
}

sans-titre.png (5.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by alpapan · Dec 24, 2019 at 11:00 PM

Resurrecting as this is a top google hit... use this:

GetComponent<Image>().alphaHitTestMinimumThreshold = 1f; // or anything > 0

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Phyrik · Jun 03, 2020 at 02:26 PM 2
Share

Remember to set the image asset's read write enable checkbox to true

avatar image
0

Answer by malkere · May 13, 2016 at 11:53 AM

http://forum.unity3d.com/threads/raycast-alpha-information.24159/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by seguso · Apr 14, 2017 at 04:32 PM

Here is the solution: use RaycastAll to retrieve all the objects hit, then return the closest object such that the alpha value of the pixel is not 0.

 private static RaycastHit? RaycastWithTransparency(Ray ray)
 {

     var res = Physics.RaycastAll(ray, float.MaxValue).ToList().OrderBy(h => h.distance);


     foreach (var h in res)
     {
         var col = h.collider;


         Renderer rend = h.transform.GetComponent<Renderer>();
         Texture2D tex = rend.material.mainTexture as Texture2D;
         var xInTex = (int) (h.textureCoord.x*tex.width);
         var yInTex = (int) (h.textureCoord.y*tex.height);
         var pix = tex.GetPixel(xInTex, yInTex);

         if (pix.a > 0)
         {
             //Debug.Log("You hit: " + col.name + " position " + h.textureCoord.x + " , " + h.textureCoord.y);
             return h;
             
         }
     }
     return null;
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hazneliel · Apr 27, 2018 at 07:33 PM 0
Share

This does not work with UI since they dont have colliders and EventSystem.current.RaycastAll(ray, res); is used

How can you deter$$anonymous$$e the transparency this way?

Thanks

avatar image malkere Hazneliel · Apr 28, 2018 at 02:28 AM 0
Share

if that is true using EventSystem.current.RacastAll(ray, res); with seguso's answer should work. You basically want to hit the UI object, grab the Texture2D, calculate the mouses position on the Texture2D, deter$$anonymous$$e if that part of the Texture2D is transparent or not.

avatar image Hazneliel malkere · Apr 28, 2018 at 03:57 AM 0
Share

But EventSystem.current.RacastAll returns a List of RaycastResult and from a RaycastResult it is not possible to access the Texture2D as far as I know

Show more comments
avatar image seguso Hazneliel · Apr 28, 2018 at 09:03 AM 0
Share

Sorry, I don't remember. It might be I was not using UI at all. (I might have missed the "ui" part.)

avatar image
0

Answer by Hazneliel · Apr 27, 2018 at 07:34 PM

Using Physics.RaycastAll does not work with UI since they dont have colliders and EventSystem.current.RaycastAll is used

How can you determine the transparency this way?

Thanks

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davedev · Oct 19, 2018 at 10:10 AM 0
Share

A technique used in other applications is to get the position of the raycast and then use getpixel to check the color of the pixels at that position. Not simple but probably possible...

avatar image
0

Answer by w34edrtfg · Nov 08, 2021 at 08:59 AM

Script for RawImages that acts like alphaHitTestMinimumThreshold :

 using System;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 /*
  * RawImage doesn't have alphaHitTestMinimumThreshold, (which let's you avoid raycasting transparent pixels)
  * This class act's as alphaHitTestMinimumThreshold replacement for RawImages by overriding raycast's default behaviour
  * THE IMAGE TEXTURE MUST HAVE READ/WRITTING IN IT'S SETTINGS
  * Stolen from https://github.com/Hengle/GrassWateringSimulator2019/blob/master/Grass%20Watering%20Simulator%202019/Assets/HTC.UnityPlugin/ViveInputUtility/Examples/4.Teleport/Scripts/ImageAlphaRaycastFilter.cs
 */
 
 [RequireComponent(typeof(RawImage))]
 public class ImageAlphaRaycastFilter : UIBehaviour, ICanvasRaycastFilter
 {
     [NonSerialized]
     private RawImage m_rawImage;
 
     public float alphaHitTestMinimumThreshold = 1;
 
     protected RawImage rawImage {
         get { return m_rawImage ?? (m_rawImage = GetComponent<RawImage>()); }
     }
 
     public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) {
         if (alphaHitTestMinimumThreshold <= 0) { return true; }
         if (alphaHitTestMinimumThreshold > 1) { return false; }
 
         var texture = rawImage.mainTexture as Texture2D;
 
         Vector2 local;
         if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform, screenPoint, eventCamera, out local)) {
             return false;
         }
 
         var rect = rawImage.GetPixelAdjustedRect();
 
         // Convert to have lower left corner as reference point.
         local.x += rawImage.rectTransform.pivot.x * rect.width;
         local.y += rawImage.rectTransform.pivot.y * rect.height;
 
         // normalize
         local = new Vector2(local.x / rect.width, local.y / rect.height);
 
         try {
             return texture.GetPixelBilinear(local.x, local.y).a >= alphaHitTestMinimumThreshold;
         } catch (UnityException e) {
             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);
             return true;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing sprite alpha on all prefab clones 0 Answers

Problem with transparency with x86_64 build 1 Answer

Shader Transparency Issues 1 Answer

Standalone Player Transparency Different Than Editor,Mesh Renderer acts different in standalone game 0 Answers

Why gameobject is not transparent on Android version? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges